I'm trying to compose a string SQL query using SQLALchemy 1.1.2. I followed the explanation from the docs about using textual SQL but encountered a syntax error when I ran the following code:
from sqlalchemy.sql import text
# Create a database connection called "connection"...
q = text('USE :name')
connection.execute(q, name='DATABASE_NAME')
Here's the error message:
"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 ''DATABASE_NAME'' at line 1") [SQL: u'USE %s;'] [parameters:
(u'DATABASE_NAME',)]
Since I'm using the named colon format and passing the parameters as arguments to connection.execute I can't figure out why this problem is arising. I'm using a MySQL server, but if I read the docs correctly the text method should be DB-agnostic.
Thanks in advance for the help.
USE, just like you can't doSELECT * FROM :table. You'll have to string formatting, like'USE %s' % 'DATABASE_NAME'.textmethod can compose other types of queries but not USE or SELECT queries? EDIT: I tried it with a SELECT query, like in the docs, and got the same error.SELECT * FROM some_table WHERE id > :my_id_param, you cannot doSELECT * FROM :my_table_name_param.