使用Python插入多个MySQL记录。错误:“ Python'tuple'无法转换为MySQL类型”

时间:2019-01-21 19:57:46

标签: python mysql

使用Python插入多个MySQL记录 错误:Python'tuple'无法转换为MySQL类型

错误代码:      python python_mysql_2.py     追溯(最近一次通话):       文件“ C:\ Users \ POM \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ mysql \ connector \ conversion.py”,第181行,位于to_mysql中         返回getattr(自己,“ _ {0} _to_mysql”。格式(类型名称))(值)     AttributeError:“ MySQLConverter”对象没有属性“ _tuple_to_mysql”

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in _process_params
    res = [to_mysql(i) for i in res]
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in <listcomp>
    res = [to_mysql(i) for i in res]
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 184, in to_mysql
    "MySQL type".format(type_name))
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 "python_mysql_2.py", line 22, in <module>
    my_cursor.execute(mike_placeholders,records_list)
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 557, in execute
    psub = _ParamSubstitutor(self._process_params(params))
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 437, in _process_params
    "Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

Python代码:

#import sql.connector
import mysql.connector

#Create connection, added db we created#
connection = mysql.connector.connect(
    host='localhost', 
    user='root', 
    password='123', 
    database='testdb_1'
    ) 

#Create cursor for the connection
my_cursor = connection.cursor()

#Create SQL statement with placeholders and put in variable 
mike_placeholders="INSERT INTO users (name,email,age) VALUES (%s, %s, %s) "

#Create list (array) of records
records_list = [('Tim','Tim@tim.com',32), ('Mary','Mary@mary.com',40), ('Sam','Sam@sam.com',50), ('Fred','Fred@fred.com',22) ]

#Execute cursor, requires SQl statement variable, record variable
my_cursor.execute(mike_placeholders,records_list)

#Commit the connection to make the change on the database
connection.commit()

2 个答案:

答案 0 :(得分:1)

您无法将列表传递给my_cursor.execute(),需要遍历该列表:

for values in records_list:
    my_cursor.execute(mike_placeholders, values)

或者您可以多次重复(%s, %s, %s)并通过展平元组列表在单个查询中完成所有操作。

mike_placeholders="INSERT INTO users (name,email,age) VALUES " + ", ".join(["(%s, %s, %s)"] * len(records_list))
my_cursor.execute(mike_placeholders, sum(records_list))

答案 1 :(得分:0)

啊,我用了错误的Python术语。

在使用元组时,我应该使用“ executemany”。

my_cursor.executemany(mike_placeholders,records_list)