python 操作MySQL避坑1064


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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM