Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否从cx\U oracle执行sql脚本文件?_Python_Database_Cx Oracle - Fatal编程技术网

Python 是否从cx\U oracle执行sql脚本文件?

Python 是否从cx\U oracle执行sql脚本文件?,python,database,cx-oracle,Python,Database,Cx Oracle,是否有一种在python中使用cx_oracle执行sql脚本文件的方法 我需要在sql文件中执行create table脚本。,而cx_oracle试图遵守该脚本,但实际上没有这样的方法 然而,这个过程应该是非常直接的。将文件内容拉入字符串,按“;”字符拆分,然后调用。对结果数组的每个成员执行。我假设“;”字符仅用于分隔文件中的oracle SQL语句 f = open('tabledefinition.sql') full_sql = f.read() sql_commands = full

是否有一种在python中使用cx_oracle执行sql脚本文件的方法

我需要在sql文件中执行create table脚本。

,而cx_oracle试图遵守该脚本,但实际上没有这样的方法

然而,这个过程应该是非常直接的。将文件内容拉入字符串,按“;”字符拆分,然后调用。对结果数组的每个成员执行。我假设“;”字符仅用于分隔文件中的oracle SQL语句

f = open('tabledefinition.sql')
full_sql = f.read()
sql_commands = full_sql.split(';')

for sql_command in sql_commands:
    curs.execute(sql_command)

另一个选项是使用SQL*Plus(Oracle的命令行工具)运行脚本。您可以使用
子流程
模块从Python调用此函数-这里有一个很好的演练:

对于像
tables.sql
这样的脚本(请注意故意的错误):

您可以使用如下函数:

from subprocess import Popen, PIPE

def run_sql_script(connstr, filename):
    sqlplus = Popen(['sqlplus','-S', connstr], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    sqlplus.stdin.write('@'+filename)
    return sqlplus.communicate()
connstr
是用于
cx\U Oracle
的相同连接字符串
filename
是脚本的完整路径(例如
'C:\temp\tables.sql'
)。该函数打开一个SQLPlus会话(使用“-S”使其欢迎消息静音),然后将“@filename”排队发送给它-这将告诉SQLPlus运行脚本

sqlplus.communicate
将命令发送到stdin,等待SQL*Plus会话终止,然后作为元组返回(stdout,stderr)。使用上面的
表调用此函数。sql
将给出以下输出:

>>> output, error = run_sql_script(connstr, r'C:\temp\tables.sql')
>>> print output

Table created.

CREATE TABLER bar (
       *
ERROR at line 1:
ORA-00901: invalid CREATE command

>>> print error

这将需要一点解析,具体取决于您希望返回到程序其余部分的内容-如果是交互式的,您可以向用户显示整个输出,如果您只想检查它是否正常运行,则可以扫描“ERROR”一词。

上拆分将失败;这可能需要一点简单的扫描;“字符仅用于分隔文件中的oracle SQL语句。”不过,您更清楚地说明了明显的问题。谢谢还假定唯一的分隔符是
。这通常是这样的,但是如果sql脚本包含存储过程之类的内容,那么在定义过程之前,分隔符会被更改……接受的答案在PL/sql中不起作用。。在这种情况下,这个sqlplus位没有问题。
>>> output, error = run_sql_script(connstr, r'C:\temp\tables.sql')
>>> print output

Table created.

CREATE TABLER bar (
       *
ERROR at line 1:
ORA-00901: invalid CREATE command

>>> print error