1064 (42000): 你的 SQL 语法有误

1064 (42000): You have an error in your SQL syntax

我期待此函数的 True/False,它会检查手机号码是否存在于数据库中?

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

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

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

试试这个方法

The following code converts args to a tuple type.

args = (mobile,)