pymysql-是否可以将变量用作“哪里”参数?

时间:2018-12-19 12:16:43

标签: python pymysql

这是我的代码:

def lookfor(var1,var2):

try:
    with connection().cursor() as cursor:
        sql = """SELECT * FROM servers WHERE %s = %s"""

        cursor.execute(sql, (var1, var2))
        result = cursor.fetchall()

        for x in result:
            print(x)
finally:
    connection().close()

lookfor(“优先级”,“重要”)

-

我得到0条结果,没有错误...:(

请帮助,必须有一种方法。

1 个答案:

答案 0 :(得分:0)

您可以使用字符串插值:

with connection().cursor() as cursor:
        sql = "SELECT * FROM servers WHERE " + str(var1) + " = " + str(var2)

        cursor.execute(sql)
        result = cursor.fetchall()