我正在使用 SQL alchemy 中的 Text 对象的参数化查询,并得到不同的结果。
工作示例:
import sqlalchemy as sqlal
from sqlalchemy.sql import text
db_table = 'Cars'
id_cars = 8
query = text("""SELECT *
FROM Cars
WHERE idCars = :p2
""")
self.engine.execute(query, {'p2': id_cars})
产生 sqlalchemy.exc.ProgrammingError 的示例: ( pymysql.err.ProgrammingError ) (1064, "You have an error in your SQL syntax)
import sqlalchemy as sqlal
from sqlalchemy.sql import text
db_table = 'Cars'
id_cars = 8
query = text("""SELECT *
FROM :p1
WHERE idCars = :p2
""")
self.engine.execute(query, {'p1': db_table, 'p2': id_cars})
关于如何使用动态表名运行查询的任何想法,这些表名也受到 sql 注入的保护?