参数化查询在python和mysql数据库中不起作用

时间:2018-07-07 19:47:01

标签: python mysql sql

import mysql.connector
conn=mysql.connector.connect(user="root",password="",host="localhost",port="3306",database="ddp")
cur=conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS Employer( CompanyID varchar(5) PRIMARY KEY, CompanyName varchar(50) NOT NULL, City varchar(15))')
#cur.execute("INSERT INTO Employer(CompanyID,CompanyName, City) VALUES ('a123','TATA','MUMBAI')")
id1="a123"
cur.execute("SELECT * FROM Employer WHERE CompanyID = %s",(id1))
print(cur.fetchall())
#print(cur.description)
conn.commit()
cur.close()
conn.close()
  

这是我上面的代码,我正在尝试使用通过变量id1给出的值的select语句,但它无法正常工作并给出以下错误

Traceback (most recent call last):
  File "C:/Users/DELL/Desktop/PYTHON/module2/check.py", line 7, in <module>
    cur.execute("SELECT * FROM Employer WHERE CompanyID = %s",(id1))
  File "F:\PYTHON_3.5\lib\site-packages\mysql\connector\cursor_cext.py", line 246, in execute
    prepared = self._cnx.prepare_for_mysql(params)
  File "F:\PYTHON_3.5\lib\site-packages\mysql\connector\connection_cext.py", line 520, in prepare_for_mysql
    raise ValueError("Could not process parameters")
ValueError: Could not process parameters

0 个答案:

没有答案