请解释。
我正在使用此语句,但是出现无效的SQL语句错误。
query =“ BEGIN” +“ \ n” +“ dbms_stats.gather_table_stats(” + schema_name +“,” + table_name +“,” +“级联=> TRUE,度=> 4)\;”查询+ =“ \ n” +“ END \;”
cur.execute(query)
打印输出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