我正在尝试为数据库编写一个简单的类,该类可以通过任何其他python程序作为API进行访问。 该函数应该使用以下命令(在mysql命令行上有效)在表中插入新行:
INSERT INTO Tweethistory (LastUsed) VALUES ('2018-12-01 12:12:12');
运行相同的python代码如下:
def add_user(self, time):
self.cursor.execute("""
INSERT INTO Tweethistory (LastUsed)
VALUES (%s)
""", (time))
self.db.commit()
return int(self.cursor.lastrowid)
从具有以下代码的另一个程序中调用该函数:
db = MyDatabase()
time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
ID = db.add_user(time)
db.close_connection() #Function to disconnect database
运行程序时,遇到以下错误:
File "/home/ece-student/Documents/601/MiniProject1/Database.py", line 28, in add_user
""", (time))
File "/home/ece-student/anaconda3/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/ece-student/anaconda3/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 535, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
我已经尝试了所有方法,从使用“查询”变量存储字符串并调用cursor.execute(query, [time])
到使用单引号将强值表示为“%s”,但似乎无济于事。
任何帮助将不胜感激!