0

I need to create a database from within a Python/SQLAlchemy script, so to follow the safest practices I wanted to use query parameters (note: technically this would not be required, as the database name is hardcoded). So I wrote:

session.execute(text(f"CREATE DATABASE IF NOT EXISTS :db;"), {"db": "dbname"})

However, this returns an error:

sqlalchemy.exc.ProgrammingError: (pymysql.err.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 ''dbname'' at line 1")
[SQL: CREATE DATABASE IF NOT EXISTS %(db)s;]
[parameters: {'db': 'dbname'}]
(Background on this error at: https://sqlalche.me/e/14/f405)

How should I write it then?

baggiponte
  • 547
  • 6
  • 13

0 Answers0