pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near xxxxx)
pymysql操作數據庫出現這種錯誤多半是語法問題,比如當我們使用如下語法是就會出現此錯誤
conn.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s" % (param1, param2))
正確的寫法如下
c.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))
拓展:
python操作MySQL數據庫
import pymysql db = pymysql.connect(host='localhost', user='root', password='123456', port=3306, db='spiders',charset='utf8') cursor = db.cursor()
sql = 'select * from students;'
cursor.execute(sql)
cursor.close()
db.close()