python3 mysql连接器模块引发异常-ValueError:无法处理参数

时间:2019-04-01 05:04:03

标签: python mysql

我正试图克服困扰我的错误。我构建了一个简单的脚本来填充单列数据库。

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="password",
  database="mydatabase"
) 
mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE IF NOT EXISTS data(pass_word VARCHAR(20))")

val = 'test'
sql = "INSERT INTO data(pass_word) VALUES '%s'"
mycursor.execute(sql, (val))
mydb.commit() 

它创建表没有问题,所以我知道连接器正在工作。但是它拒绝将val插入pass_word中。

它引发以下异常

Press ENTER or type command to continue
Traceback (most recent call last):
  File "sql-try.py", line 19, in <module>
    mycursor.execute(sql, (val))
  File "/usr/local/lib/python3.6/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
    prepared = self._cnx.prepare_for_mysql(params)
  File "/usr/local/lib/python3.6/dist-packages/mysql/connector/connection_cext.py", line 538, in prepare_for_mysql
    raise ValueError("Could not process parameters")
ValueError: Could not process parameters

1 个答案:

答案 0 :(得分:-1)

我认为问题是我的表只有一列,所以当我传递val时,val本身必须是一个元组。将其更改为(sql,(val,))不能解决该问题。当我创建一个包含多个列的表时,问题就消失了。

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="password",
  database="mydatabase"
)
mycursor = mydb.cursor()
myname = 'jimmy'
password = 'grizwald'

mycursor.execute("CREATE TABLE IF NOT EXISTS data3(my_name VARCHAR(20), pass_word VARCHAR(20))")

val = (myname, password )
sql = "INSERT INTO data3(my_name, pass_word) VALUES (%s, %s)"
mycursor.execute(sql, val)
mydb.commit()