简体   繁体   English

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

[英]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 . 它失败,并显示错误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: 我不认为此错误与绑定参数有任何关系,因为没有表创建的简单select语句会顺利运行:

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. DDL语句中不允许绑定变量。 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. 这就是为什么它可以通过简单查询按预期运行的原因,但是一旦有了create table语句,它就会失败。 You will have to write your DDL statement without any bind variables, unfortunately! 不幸的是,您将必须编写没有任何绑定变量的DDL语句!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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