2017-04-27 28 views
-2

我想加載文件(.txt)到db2數據庫,使用db2連接到MyDB並從myfile.txt加載del插入到Schema.MyTable >> myfile.txt.log.I使用python來自動完成這項工作。代碼如下所示。使用python從txt加載到db2,但失敗。爲什麼?

for s in os.listdir(os.chdir("/home/db2inst1/preDB/201701/")): 
    print(os.getcwd()) 
    try: 
     ........ 
     os.system(cntdb2)#cntdb2= db2 connect to MyDB2 
     os.system(loadcmd)#loadcmd= db2 load from 'myfile.txt' of del insert into "Schema"."MyTable" >>"myfile.txt.log" 

    except exception as e: 
     print("Failed to read this file!") 
     print(e) 

但我得到了 SQL1024N數據庫連接不存在的消息。 SQLSTATE = 08003 當我執行命令 db2連接到MyDB2 db2從del的'myfile.txt'加載插入到「Schema」。「MyTable」>>「myfile.txt.log」 我可以獲取數據加載。

我犯了什麼錯誤?

+0

執行他們我有後援使用os.system執行命令(字符串)在一個子shell中。這是通過調用標準C函數system()來實現的,並且具有相同的限制......所以,我需要添加&& or ;來組合這兩個命令。 –

+0

注意:如果將環境變量'DB2DBDFT'設置爲數據庫的名稱,則DB2將建立隱式連接,從而不需要發出'connect to ...'語句。 –

回答

0

同時運行兩個命令,你可以把它們放入一個文本文件:

connect to MyDB2 
load from 'myfile.txt' ... 

os.system('db2 -f /path/to/script.file') 
+0

謝謝。它很有用。 –

相關問題