Python 如何修复编程错误:(1064,“您的SQL语法有错误)?

Python 如何修复编程错误:(1064,“您的SQL语法有错误)?,python,mysql,Python,Mysql,我正在使用pymysql从MySQL数据库中获取数据,但出现此错误 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 '23:59:00' at line 1") 我的代码如下 cursor1 = price_db.cursor

我正在使用pymysql从MySQL数据库中获取数据,但出现此错误

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 '23:59:00' at line 1")
我的代码如下

cursor1 = price_db.cursor()
cursor1.execute('select * from bo_prices.chart_prices where price_date = {};' .format(string_time)) # Extract 'chart_price' table from price db
data7 = cursor1.fetchall()
chart_price = pd.DataFrame(data7)
字符串时间为

string_time = srt(datetime.datetime.combine(datetime.date(2019,10,07), datetime.time(23,59)))

如何修复它以获取数据?谢谢!

日期值没有被引用,只需将单引号添加到where子句{}

cursor1.execute("select * from bo_prices.chart_prices where price_date = '{}';" .format(string_time))
您永远不应该对SQL查询使用字符串格式

你应该使用替换

q=从bo_prices.chart_prices中选择*,其中price_date= v=字符串时间, 游标1.executeq,v
实际上,

…其中price_date='{}“add Quotes我尝试使用.format表单,因为日期每天都在变化,我希望代码自动运行。您需要用于字符串或转义”此解决方案不安全,允许SQL注入漏洞。@KlausD。解决方案只是修复现有发布代码中的错误。问题不在于最佳做法。问题总是关于bes不要练习。你不会给出包含其他严重问题的解决方案。不需要元组,只需使用cursor1.executeq,string\u time即可。