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

标签 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()
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 是

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

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

最佳答案

您不应该对 SQL 查询使用字符串格式。

你应该使用替换。

q = "select * from bo_prices.chart_prices where price_date = '(?)'"
v = (string_time,)
cursor1.execute(q, v) 

实际上是 documentation of parameter substitution is quite good.

关于python - 如何修复编程错误 : (1064,“您的 SQL 语法有错误)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391586/

相关文章:

python - 关于通过(unix-)套接字发送/接收大量数据的另一个困惑

python - PyUSB 找不到设备

python - 如何并行迭代 SimPy 模拟?

python - 如何将游戏数据导出到MySQL?

MySql 求和别名

php - 使用php以excel xml格式导出期间增加行限制

mysql - SQL 查询建议(用连接替换存在)

MySQL用户密码问题

python - 使用 python 将 Spark 2 与 HBase 集成连接的 jars

mysql - 根据修改的列对 Mysql 结果进行排序