Python 我可以使用SQLPlus执行.sql文件吗?

Python 我可以使用SQLPlus执行.sql文件吗?,python,oracle,sqlplus,Python,Oracle,Sqlplus,我有一个.sql文件,其代码如下: delete from stalist where stalistid=4944 / insert into stalist (stalistid, staid) (select distinct 4944, staid from staref Where staid in( 3797,3798, 3870,4459, 3871,3872, 3876,3877, 0 )) / commit / 我想使用Python从SQLPlus执行这个文件。这可能吗?我在

我有一个.sql文件,其代码如下:

delete from stalist
where stalistid=4944
/
insert into stalist
(stalistid, staid)
(select distinct 4944, staid
from staref
Where staid in(
3797,3798,
3870,4459,
3871,3872,
3876,3877,
0
))
/
commit
/
我想使用Python从SQLPlus执行这个文件。这可能吗?我在这类工作中没有任何python经验,需要一些帮助。谢谢大家!

请参见本教程:

i、 e


应该这样做(其中sqlCmmand是“@scriptname.sql”)。

下面是一个如何做到这一点的示例。您需要读取文件并将整个字符串作为命令传递

(username, password, host) = ("user","password","host") 

conn_string = " %s/%s@%s "% (username,password,host)
session = Popen(['sqlplus','-S', conn_string], stdin=PIPE, stdout=PIPE, stderr=PIPE)
logging.info("Starting sqlplus")

sql_file = '%s/%s' % (sql_folder, file)
logging.info("Running " + sql_file)
f= open(sql_file,'r')
cmd = f.read()
session.stdin.write(cmd)

stdout, stderr = session.communicate()

因此,脚本可以正确运行,但.sql没有执行它应该执行的操作。如果打开SQLPLUS并在程序中运行该文件,它将正确填充oracle表,但使用脚本不会填充该表。知道为什么会这样吗?运行脚本时是否需要SQLPLUS open?我打开了文件f=open('mysql.sql','rb')cmd=f.read(),它成功运行,但没有从sqlfile执行ddl
(username, password, host) = ("user","password","host") 

conn_string = " %s/%s@%s "% (username,password,host)
session = Popen(['sqlplus','-S', conn_string], stdin=PIPE, stdout=PIPE, stderr=PIPE)
logging.info("Starting sqlplus")

sql_file = '%s/%s' % (sql_folder, file)
logging.info("Running " + sql_file)
f= open(sql_file,'r')
cmd = f.read()
session.stdin.write(cmd)

stdout, stderr = session.communicate()