Python Mysql怎么多条件搜索内容?

请问Python Mysql怎么使用多条件搜索内容呢? 为什么我使用字符串拼接后搜索会报错呢? 搜索的两个字段都是varchar

cur = con_mysql.cursor()

#能正确返回结果
search_info = "select * from db_log where log_time = '2022-08-19 07:00:00至2022-08-19 20:30:00'"
search_info = "select * from db_log where log_area = '西南路一段'"

#未查询到,返回空
search_info = "select * from db_log where log_time = '2022-08-19 07:00:00至2022-08-19 20:30:00' and log_area = '西南路一段'"

#报错
#(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 \'"select * from db_log where log_time = 2022-08-19 07:00:00至2022-08-1\' at line 1')
#"select * from db_log where log_time = 2022-08-19 07:00:00至2022-08-19 20:30:00"
aaa = "2022-08-19 07:00:00至2022-08-19 20:30:00"
search_info = '''"select * from state_db_log where log_time = ''' + aaa + '''"'''

cursor.execute(search_info)
res_search = cursor.fetchall()
print(res_search)

拼接字符串不需要把前后的双引号也拼接进字符中
改用 f-字符串 方便一些

aaa = "2022-08-19 07:00:00至2022-08-19 20:30:00"
search_info = f"select * from db_log where log_time = '{aaa}'"

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

拼接这个字符串少了引号,所以报错


aaa = " '2022-08-19 07:00:00至2022-08-19 20:30:00' "
search_info = "select * from db_log where log_time = " + aaa