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

Failing to create table using bound parameters in 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

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

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

"create table" 加上 "bound parameters" 中似乎有什么东西中断了查询。知道为什么会发生这种情况以及如何解决它吗?

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