Hi All, I've been struggling with writing a comment to the database. The table only has ID and content columns, from the tutorial. Because I found that SqlAlchemy didn't handle simple dates like I want, I reverted to interacting directly with the database. I have no problems writing to other tables of numerous columns, but this Comments table with just one data column is frustrating me.
My Code:
import pytz # datetime conversion
import dbaccess as DBconfig
import mysql.connector
#establishing the connection
conn = mysql.connector.connect(
user=DBconfig.username, password=DBconfig.password, host=DBconfig.hostname, database=DBconfig.databasename)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
# Prepare SQL query to INSERT a record into the balance table
comment_sql = "INSERT INTO comments (content) values (%s)"
# current date and time
from datetime import datetime
timezone = pytz.timezone('Europe/Berlin')
now = datetime.utcnow().astimezone(tz=timezone).strftime('%Y-%m-%d %H:%M')
message = str(now) + " Received request"
val = message
cursor.execute(comment_sql,val)
conn.commit()
conn.close()
This code is what I believe I'm generating and it works perfectly in the mysql interface, writing the row as expected:
INSERT INTO comments (content) values ('2022-07-22 20:55:52 Received request");
But in the console window of the Python code, I#m getting this error:
line 23: cursor.execute(comment_sql,val)
Traceback (most recent call last):
File "/home/pegasus163/mysite/write_comment.py", line 23, in <module>
cursor.execute(comment_sql,val)
File "/usr/local/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 649, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
Is there some strange issue with just one substitution variable?
mysql> describe comments;
+---------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| content | varchar(4096) | YES | | NULL | |
+---------+---------------+------+-----+---------+----------------+
Thanks for your help