在Python 3.3中使用%s进行SQL查询 [英] SQL query using %s in Python 3.3
本文介绍了在Python 3.3中使用%s进行SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从MySQL数据库检索数据.
I'm trying to retrieve data from a MySQL-database.
A = "James"
query = ("SELECT * FROM DB.tblusers WHERE UserName = %s ")
c = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='DB')
cur1 = c.cursor()
cur1.execute(query, A)
给出以下错误信息:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%' at line 1
但是SQL在mySQL Workbench中可以工作. 有什么想法吗?
But the SQL works in the mySQL Workbench. Any ideas?
推荐答案
A
应该是一个元组,尝试使用A = ("James",)
A
should be a tuple, try with A = ("James",)
参见 MySQLCursor.execute(operation,params =无,multi = False)
感谢"swordofpain"(我学到了一些东西),添加了逗号
added a comma, thanks to "swordofpain" (I learned something)
这篇关于在Python 3.3中使用%s进行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文