【问题标题】:1064 (42000): You have an error in your SQL syntax1064 (42000): 你的 SQL 语法有错误
【发布时间】:2021-05-09 06:40:13
【问题描述】:

我期待这个函数的真/假,它检查手机号码是否存在于数据库中?

def find(self, mobile):
    query = "select id from table_customer " \
            "WHERE mobile = %s"
    args = (mobile)
    resp = False

    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        cursor = conn.cursor(buffered=True)
        cursor.execute(query, args)

        if cursor.rowcount == 1:
            resp = True

    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()

    return resp

当我执行时它给了我这个错误:

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 '%s' at line 1

使用硬编码的手机号码没有问题。

为什么上面的代码会抛出语法错误?

【问题讨论】:

  • 手机号码是什么字段类型?我希望它不是一个数字。如果是字符串,则需要将参数放入单引号中。

标签: mysql python-3.x mysql-connector


【解决方案1】:

试试这个方法

以下代码将args 转换为tuple 类型。

args = (mobile,)

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    相关资源
    最近更新 更多