【发布时间】:2016-08-04 08:47:33
【问题描述】:
我在通过 cx_oracle 运行一些 .sql 文件时遇到了实际问题。例如,如果我通过 Oracle Developer 运行,下面的 test_table2.sql 可以完美运行。
declare
c int;
begin
select count(*) into c from user_tables where table_name = upper('TEST2');
if c = 1 then
execute immediate 'drop table TEST2';
end if;
EXECUTE IMMEDIATE 'CREATE TABLE MURRAYLR.test2 as
select * from Dans_Table';
EXECUTE IMMEDIATE'CREATE TABLE MURRAYLR.test1
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
)';
end;
Python 代码通常适用于简单查询,但一旦我尝试检查现有表脚本,它就会给我错误。见下文
Python 2.7.11 代码
import sys
import cx_Oracle
connection = cx_Oracle.connect('user','password','serv')
cursor = connection.cursor()
filename="C:\Users\desktop\Test_table2.sql"
f = open(filename)
full_sql = f.read()
sql_commands = full_sql.replace('\n', '').split(';')[:-1]
for sql_command in sql_commands:
cursor.execute(sql_command)
connection.close()
错误信息
cursor.execute(sql_command) 数据库错误:ORA-06550:第 1 行,第 15 列: PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:
:= 。 ( @ % ; 非空范围默认字符
【问题讨论】:
标签: python oracle python-2.7 cx-oracle