2

次のコードを含む .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 の経験がなく、助けが必要です。ありがとうございました!

4

2 に答える 2

8

このチュートリアルを参照してください: 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()

実行する必要があります (sqlCmmand は「@scriptname.sql」です)。

于 2012-11-26T15:40:09.400 に答える