简体   繁体   中英

Failing to create table using bound parameters in sqlalchemy/cx_oracle

I want to execute a "create table" statement in-database using bound parameters. This works (without bound parameters):

from sqlalchemy.sql import text
from sqlalchemy import create_engine

con = create_engine(\\..)
s = text("""create table test_table as select * from dual where 1 = 1 """)
con.execute(s)

However, if I use bound parameters:

s = text("""create table test_table as select * from dual where 1 = :a """)
con.execute(s, a = 1)

it fails with error DatabaseError: (cx_Oracle.DatabaseError) ORA-01036: illegal variable name/number .

I am not convinced this error has anything to do with the bound parameters because a simple select statement without table creation will work smoothly:

s = text("""select * from dual where 1 = :a """)
con.execute(s, a = 1).fetchall()
#[('X',)]

There seems to be something in the "create table" plus "bound parameters" that breaks the query. Any idea why this is happening and how to fix it?

Bind variables are not permitted in DDL statements. This is the reason why it works as expected with a simple query, but as soon as you have a create table statement, it fails. You will have to write your DDL statement without any bind variables, unfortunately!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM