MySQL 问题。如何在表是变量的“select * from table”中传输表名? [关闭]

问题描述 投票:0回答:0

我的 Python 脚本有问题,它应该从 MySQL 数据库返回数据。执行查询是

f"select post_date from {table} where date(post_date) >= %s and date(post_date) <= %s order by post_date desc"

但是会产生错误

(1054, "Unknown column 'post*date' in 'field list'")

(1064, syntax error... near* ''name_of_database') The problem is with {table}.

如何将表名传输到 sql-query 中?我使用了几种变体:%s、f-strings、.format()、字符串连接('from' + table + 'where...')但它不起作用

附言如果我在 IDE 中运行脚本,则可以正常工作。但是,如果我尝试启动 Flask 应用程序,它会失败

python mysql sql mysql-error-1064 mysql-error-1054
© www.soinside.com 2019 - 2024. All rights reserved.