The error occurs when looping through a tuple and inserting data into a MySQL Database
In earlier segments of the code I have selected multiple FilmID's from a database and wish to reinsert the FilmID into a different table in the database. I am using the MySQL.Connector module for python and I am running into the following error:
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
The code where the error I believe to be is below:
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()
I initially thought that this was a error with the datetime so I tried ommiting it only to get the same result. I hope what I have given is sufficient as this is my first time on stack overflow. If anyone has any solutions to my problem I am very grateful.
Thank you very much, Lupin
That's because variable "i" is passed as a tuple in other words when you loop in the list "myresult" the value of "i" is (*)
where * is the value of id.
So you have to select the index of the first item:
vals = (UserID, i[0], datetime.datetime)