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

Ste*_*e83 1 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
/
Run Code Online (Sandbox Code Playgroud)

我想使用Python从SQLPlus执行此文件.这可能吗?我在这类工作中没有任何python经验,可以使用一些帮助.谢谢!

Daz*_*zaL 7

请参阅本教程:http://moizmuhammad.wordpress.com/2012/01/31/run-oracle-commands-from-python-via-sql-plus/

from subprocess import Popen, PIPE

#function that takes the sqlCommand and connectString and retuns the output and #error string (if any)

def runSqlQuery(sqlCommand, connectString):

session = Popen(['sqlplus', '-S', connectString], stdin=PIPE, stdout=PIPE, stderr=PIPE)
session.stdin.write(sqlCommand)
return session.communicate()
Run Code Online (Sandbox Code Playgroud)

应该这样做(其中sqlCmmand是"@ scriptname.sql").