我的问题涉及在Object.entries(obj1).some(([key1, value1])=>Object.entries(obj2).some(([key2, value2])=>key1===key2 && value1===value2))
下面传递一个字符串
cursor.execute
我收到错误
import pymsyql
import json
connection = pymysql.connect(
host='localhost', user='u_u_u_u_u',
password='passwd', db='test',
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor
)
def get_data(table):
try:
with connection.cursor() as cursor:
sql = """
SELECT * FROM %s;
"""
cursor.execute(sql, (table,))
result = cursor.fetchall()
return json.dumps([dict(ix) for ix in result])
except (TypeError, pymysql.err.ProgrammingError) as error:
print(error)
finally:
pass
get_data('table_1')
connection.close()
似乎pymysql.err.ProgrammingError: (1064, "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 ''table_1'' at line 1")
不希望将字符串作为参数传递;当我直接输入字符串时,如execute
,我得到同样的错误。
我对导致问题的原因感到疑惑,双引号cursor.execute(sql, ('table_1',))
令人困惑。谁能告诉我这里发生了什么?
答案 0 :(得分:2)
您不能将表名作为参数传递,唉。你必须把它变成查询字符串:
sql = """
SELECT * FROM `{0}`;
""".format(table)
cursor.execute(sql)