循环通过元组并将数据插入MySQL数据库时会发生错误
在代码的前面部分中,我从数据库中选择了多个FilmID,并希望将FilmID重新插入数据库中的另一个表中。我正在使用python的MySQL.Connector模块,遇到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\conversion.py", line 179, in to_mysql
return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 417, in _process_params
res = [to_mysql(i) for i in res]
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 417, in <listcomp>
res = [to_mysql(i) for i in res]
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\conversion.py", line 181, in to_mysql
raise TypeError("Python '{0}' cannot be converted to a "
TypeError: Python 'tuple' cannot be converted to a MySQL type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "Z:\My Files\Student My Documents\A Level\Computing\NEA\nea2.py", line 170, in <lambda>
button1 = tk.Button(window, text="Start Personality Quiz?", command=lambda: menu("1"), width=18)
File "Z:\My Files\Student My Documents\A Level\Computing\NEA\nea2.py", line 17, in menu
PersonalityQuiz()
File "Z:\My Files\Student My Documents\A Level\Computing\NEA\nea2.py", line 82, in PersonalityQuiz
mycursor.execute(LinkInsert,vals)
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 539, in execute
psub = _ParamSubstitutor(self._process_params(params))
File "C:\Users\lupin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 421, in _process_params
raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type
我认为错误所在的代码如下:
import mysql.connector
import datetime
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="",
database="database name")
sql = "SELECT FilmID FROM Movies"
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
firstname=input
surname=input
SQLinsert = "INSERT INTO users (FirstName, Surname) VALUES (%s,%s)"
vals = (firstname,surname)
mycursor.execute(SQLinsert,vals)
mydb.commit()
UserID = mycursor.lastrowid
for i in myresult:
LinkInsert = "INSERT INTO userlink VALUES (%s,%s,%s)"
vals = (UserID, i, datetime.datetime)
mycursor.execute(LinkInsert,vals)
mydb.commit()
我最初认为这是datetime的一个错误,所以我尝试对其进行修改,但结果相同。我希望我给出的足够了,因为这是我第一次讨论堆栈溢出。如果有人能解决我的问题,我非常感激
非常感谢你,卢平
这是因为变量“i”作为元组传递,换句话说,当您在列表“myresult”中循环时,“i”的值是
(*)
,其中*是id的值因此,您必须选择第一项的索引:
您正在传递未转换为任何MySQL类型的
vals
元组所以您可以像这样在
executemany
中传递元组:相关问题 更多 >
编程相关推荐