MySQL Connector无法处理参数 [英] MySQL Connector could not process parameters
问题描述
我试图遍历数组并将每个元素插入表中.据我所知,我的语法是正确的,我直接从 Microsoft Azure的文档.
I'm trying to loop through an array and insert each element into a table. As far as I can see my syntax is correct and I took this code straight from Microsoft Azure's documentation.
try:
conn = mysql.connector.connect(**config)
print("Connection established")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with the user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = conn.cursor()
data = ['1','2','3','4','5']
for x in data:
cursor.execute("INSERT INTO test (serial) VALUES (%s)",(x))
print("Inserted",cursor.rowcount,"row(s) of data.")
conn.commit()
cursor.close()
conn.close()
print("Done.")
当我运行时,它进入cursor.execute(...)
,然后失败.这是堆栈跟踪.
When I run this is gets to cursor.execute(...)
and then fails. Here is the stack trace.
回溯(最近通话最近): 在第29行的文件"test.py"中 cursor.execute(插入测试(串行)值(%s)",(测试")) 执行中的文件"C:\ Users \ AlexJ \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ mysql \ connector \ cursor_cext.py",执行 准备= self._cnx.prepare_for_mysql(参数) 在prepare_for_mysql中的行538中的文件"C:\ Users \ AlexJ \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ mysql \ connector \ connection_cext.py" 引发ValueError(无法处理参数") ValueError:无法处理参数
Traceback (most recent call last): File "test.py", line 29, in cursor.execute("INSERT INTO test (serial) VALUES (%s)",("test")) File "C:\Users\AlexJ\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor_cext.py", line 248, in execute prepared = self._cnx.prepare_for_mysql(params) File "C:\Users\AlexJ\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 538, in prepare_for_mysql raise ValueError("Could not process parameters") ValueError: Could not process parameters
推荐答案
尝试一下:
for x in data:
value = 'test'
query = "INSERT INTO test (serial) VALUES %s"
cursor.execute(query,(value,))
print("Inserted",cursor.rowcount,"row(s) of data.")
由于您使用的是mysql模块,因此cursor.execute
需要一个sql查询和一个元组作为参数
Since you are using mysql module, cursor.execute
requires a sql query and a tuple as parameters
这篇关于MySQL Connector无法处理参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!