【问题标题】:AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'AttributeError:“MySQLConverter”对象没有属性“_tuple_to_mysql”
【发布时间】:2021-03-25 12:27:32
【问题描述】:

循环遍历元组并将数据插入 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()

我最初认为这是日期时间的错误,所以我尝试省略它只是为了得到相同的结果。我希望我给出的内容已经足够了,因为这是我第一次发生堆栈溢出。如果有人对我的问题有任何解决方案,我将不胜感激。
非常感谢你,卢平

【问题讨论】:

    标签: python mysql python-3.x mysql-connector-python


    【解决方案1】:

    这是因为变量“i”作为元组传递,换句话说,当您在列表“myresult”中循环时,“i”的值是(*),其中 * 是 id 的值。

    所以你必须选择第一项的索引:

    vals = (UserID, i[0], datetime.datetime)
    

    【讨论】:

      【解决方案2】:

      您正在传递vals 元组,该元组未转换为任何 MySQL 类型。

      所以你可以像这样在executemany 中传递元组:

      mycursor.executemany(LinkInsert,vals)
      

      【讨论】:

        猜你喜欢
        • 2016-06-24
        • 2012-12-01
        • 2021-04-19
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多