我们可以使用cx_Oracle在python3中实现查询dbms_stats.gather_table_stats。如果是,那么如何?

问题描述 投票:0回答:1

请解释。

我正在使用此语句,但是出现无效的SQL语句错误。

query =“ BEGIN” +“ \ n” +“ dbms_stats.gather_table_stats(” + schema_name +“,” + table_name +“,” +“级联=> TRUE,度=> 4)\;”查询+ =“ \ n” +“ END \;”

cur.execute(query)

python-3.x cx-oracle
1个回答
0
投票

打印输出query。您可能会在分号之前注意到一些额外的反斜杠。并且您将看到未加引号的模式和表名。简单的重写是:

query = "BEGIN"+"\n"+"dbms_stats.gather_table_stats('"+schema_name+"' , '"+table_name+"' , "+"cascade=>TRUE, degree=>4);"
query +="\n"+"END;"

但是将数据串联到SQL中会带来安全风险,还会影响可伸缩性。您应该使用绑定变量,例如:

schema_name = 'HR'
table_name = 'TESTTABLE'
query = "BEGIN dbms_stats.gather_table_stats(:sn, :tn, cascade=>TRUE, degree=>4); END;"
cur.execute(query, sn=schema_name, tn=table_name)

查看cx_Oracle用户指南:https://cx-oracle.readthedocs.io/en/latest/index.html

© www.soinside.com 2019 - 2024. All rights reserved.