In my app I use SQLAlchemy and mysql-connector-python. I would like to perform such query SELECT * FROM :table LIMIT 10
on my mysql database. However my code doesn't work
table_name = "tmp1"
QUERY = "SELECT * FROM :table LIMIT 10"
conn = create_sql_connection()
res = conn.execute(
QUERY,
{'table': table_name}
).fetchall()
print(res)
I've read that you cannot use table name as a parameter and I should just use python string format. However I'm really scared that it's absolutely not safe against sql injection. How to solve it ? Is there any utility that would escape my table name variable ?
Postgres has a solution - Passing table name as a parameter in psycopg2 - Do you know how to solve it while using mysql ?