Python mysql.connector在sql语句中使用变量
我需要能够在sql语句中使用变量来搜索多个不同的列Python mysql.connector在sql语句中使用变量,python,sql,python-3.x,mysql-connector,mysql-connector-python,Python,Sql,Python 3.x,Mysql Connector,Mysql Connector Python,我需要能够在sql语句中使用变量来搜索多个不同的列 chicken = str("fav1") cursor.execute("select %s from users where usersID=%s", (chicken, userID,)) favourite_exist = str(cursor.fetchone()[0]) print(favourite_exist) 虽然此语句不产生任何错误,但它获取的是列的名称,而不是与fav1和user
chicken = str("fav1")
cursor.execute("select %s from users where usersID=%s", (chicken, userID,))
favourite_exist = str(cursor.fetchone()[0])
print(favourite_exist)
虽然此语句不产生任何错误,但它获取的是列的名称,而不是与fav1和userID=389关联的数据
任何帮助都将不胜感激 我将添加一个额外的步骤,即,
sql=“从usersID=%s的用户中选择{}”。格式化(chicken)
然后执行cursor.execute(sql,(userID,)
。如果使用print(cur.\u last\u executed)
进行检查,您将看到实际的sql查询在列名周围有引号,因此总是返回列名。打印语句的输出从usersID='389'所在的用户中选择“fav1”
。因此,正如@JustinEzequiel所建议的,首先用column准备查询,然后用parameter执行。非常感谢!我明天要试一试。