【发布时间】: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