无法使用sqlalchemy / cx_oracle中的绑定参数创建表

时间:2017-08-30 09:44:09

标签: python oracle sqlalchemy cx-oracle

我想使用绑定参数在数据库中执行“create table”语句。这适用(没有绑定参数):

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)

但是,如果我使用绑定参数:

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

失败,错误为DatabaseError: (cx_Oracle.DatabaseError) ORA-01036: illegal variable name/number

我不相信这个错误与绑定参数有关,因为没有表创建的简单select语句将顺利运行:

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

“创建表”中的某些内容加上“绑定参数”会破坏查询。知道为什么会这样,以及如何解决它?

1 个答案:

答案 0 :(得分:2)

DDL语句中不允许绑定变量。这就是为什么它通过简单的查询按预期工作的原因,但只要你有一个create table语句,它就会失败。不幸的是,您必须编写没有任何绑定变量的DDL语句!