问题遇到的现象和发生背景
使用pymysql.execute进行传参,传字符串进去,把引号也传进去了,结果就一直报错,想问一下各位大佬怎么解决?
问题相关代码,请勿粘贴截图
def find(self, condition=None, limit=0):
sql_str = ''
sql_str2 = ''
if condition:
number = 0
sql_str += 'where '
for i in condition.items():
number += 1
sql_str += 'and ' if number>1 else ''
sql_str += f'{i[0]}={i[1]} '
if limit:
sql_str2 = f'limit {limit}'
sql = 'select * from ip_agentpool %s order by score desc,speed asc %s;'
format_list = [sql_str, sql_str2]
influence_line = self.cur.execute(sql, format_list)
if influence_line:
ip_agent_pool_tuple = self.cur.fetchall()
ip_bivariate_table = pandas.DataFrame(ip_agent_pool_tuple,
columns=['ip', 'port', 'protocol', 'anonymous', 'speed', 'area', 'score', 'disable_domains'])
print('查询成功')
print(ip_bivariate_table)
else:
print('')
运行结果及报错内容
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 ''where speed=-1 ' order by score desc,speed asc 'limit 1'' at line 1")
我的解答思路和尝试过的方法
我用过字典来传参,但好像也不行