简体   繁体   English

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

[英]python3 mysql connector module throws exception - ValueError: Could not process parameters

I am trying to get past this error that is haunting me. 我试图克服困扰我的错误。 I built a simple script to populate a single column database. 我构建了一个简单的脚本来填充单列数据库。

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() 

It creates the table no problem, so I know the connector is working. 它创建表没有问题,所以我知道连接器正在工作。 But it refuses to insert the val into pass_word. 但是它拒绝将val插入pass_word中。

It throws the following exception 它引发以下异常

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

I think the issue was that my table only had one column, so when I was passing the val, val itself needed to be a tuple. 我认为问题在于我的表只有一列,所以当我传递val时,val本身必须是一个元组。 Changing it to (sql, (val,)) did not address the issue. 将其更改为(sql,(val,))不能解决该问题。 When I created a table with more than one column, the problem went away. 当我创建一个包含多个列的表时,问题就消失了。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM