python - 使用 Python 插入多个 MySQL 记录。错误 : "Python ' tu

使用Python插入多条MySQL记录 错误:Python“元组”无法转换为 MySQL 类型

错误代码:

Traceback (most recent call last):
  File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 181, 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\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()

最佳答案

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

在处理tuple 时我应该使用executemany

my_cursor.executemany(mike_placeholders,records_list)

关于python - 使用 Python 插入多个 MySQL 记录。错误 : "Python ' tuple' cannot be converted to a MySQL type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54296971/

相关文章:

postgresql - . 时如何将密码传递给 pg_dump 10主目录中的 pgpass 不是

amazon-web-services - "kubectl"未从我的本地 Windows 工作站连

haskell - 计算列表中每个元素的所有出现次数

python - 如何组合两个 Python Try 异常

reactjs - 在导航更改时滚动恢复到顶部

angular - ng2图表多图表更新

angular - 找不到自定义管道 ionic 4 |找不到管道

c# - 如何从 dapper 返回的复杂对象中修剪所有字符串

python - FlaskForm 验证码 : checking if a user alread

python - 无法使用 python 3.72 连接 firebase