How is it possible to initiate DB2 command line enviroment (Windows) and run DB2 shell EXPORT command from the Python script with waiting for results and error processing?
I tried to run
process = subprocess.call(or run)(["db2cmd", "./export.bat"],
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
OR
process = subprocess.run(or Popen)(["db2cmd",
"db2 connect to SAMPLE user XXX using XXXXX"
" & db2 EXPORT TO STAFF.ixf OF IXF SELECT * FROM STAFF"])
it works BUT
- Main Python script does not wait while DB2 Export is finished - it runs DB2CMD in the separate CMD window and ternimates immediately. DB2CMD with Export continues to run separately;
- All output streams are empty - I can't get any errors/results in this way.
What is the best appoach for Pyhton 3.8 script to
Initialize DB2 command line enviroment;
Connect to DB2 database;
Run EXPORT command and wait the results?
And in case of the error - re-connect to DB and re-run the export once again.
*Using ibm_db does not fit due big tables and complex data.
CodePudding user response:
@mao answer helped, thanks!
Add this at the beginning of the EXPORT script BAT file (what is the magic symbols?)
set DB2CLP=**$$**
In Pyhton script use 'call'. Pyhton runs and waits the completion of the EXPORT BAT
subprocess.call("\\path\\to\\export.bat" , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True )
Parse text output for errors.