pymysql - 是否可以将变量用作 "Where" 参数?
pymysql - is it possible to use a variable as a "Where" parameter?
这是我的代码:
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()
寻找("priority", "Important")
--
我得到 0 个结果并且没有错误...:(
请帮忙,一定有办法做到这一点。
您可以使用字符串插值:
with connection().cursor() as cursor:
sql = "SELECT * FROM servers WHERE " + str(var1) + " = " + str(var2)
cursor.execute(sql)
result = cursor.fetchall()
这是我的代码:
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()
寻找("priority", "Important")
--
我得到 0 个结果并且没有错误...:(
请帮忙,一定有办法做到这一点。
您可以使用字符串插值:
with connection().cursor() as cursor:
sql = "SELECT * FROM servers WHERE " + str(var1) + " = " + str(var2)
cursor.execute(sql)
result = cursor.fetchall()