【问题标题】:SQL query using %s in Python 3.3在 Python 3.3 中使用 %s 的 SQL 查询
【发布时间】:2014-01-16 01:22:06
【问题描述】:

我正在尝试从 MySQL 数据库中检索数据。

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 中工作。 有什么想法吗?

【问题讨论】:

    标签: python mysql sql


    【解决方案1】:

    A 应该是一个元组,试试A = ("James",)

    查看MySQLCursor.execute(operation, params=None, multi=False)的文档


    编辑:添加了一个逗号,感谢“swordofpain”(我学到了一些东西)

    【讨论】:

    • 非常感谢!没有“单独”工作,而是添加了另一个标准(部门)然后就可以了。
    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多