|
1 | 1 | import ibm_db |
2 | 2 |
|
3 | 3 | def main(args): |
| 4 | + #Connect to the DB2 database |
| 5 | + conn = ibm_db.connect("DATABASE=TEST;HOSTNAME=172.17.0.1;PORT=50000;PROTOCOL=TCPIP;UID=DB2INST1;PWD=db2inst1-pwd;", "", "") |
| 6 | + cmd = "DELETE FROM TESTTABLE WHERE name='Angela'" |
| 7 | + result = ibm_db.exec_immediate(conn, cmd) |
| 8 | + #output of the above can succeed or fail. Either is fine. |
| 9 | + |
| 10 | + # Insert into TestTable |
| 11 | + cmd = "INSERT INTO TESTTABLE (NAME, AGE, LOCATION) VALUES ('Angela', 27, 'Texas')" |
| 12 | + result = ibm_db.exec_immediate(conn, cmd) |
| 13 | + if not result: |
| 14 | + return {"err":"error :"+cmd} |
| 15 | + |
| 16 | + # Select from TestTable (the row just inserted) |
| 17 | + cmd = "SELECT * FROM TESTTABLE WHERE name='Angela'" |
| 18 | + result = ibm_db.exec_immediate(conn, cmd) |
| 19 | + if not result: |
| 20 | + return {"err":"error :"+cmd} |
| 21 | + else: |
| 22 | + ibm_db.fetch_both(result,0) |
| 23 | + value = ibm_db.result(result,"NAME") |
| 24 | + # Make sure the row was correctly inserted |
| 25 | + if value != 'Angela' : |
| 26 | + return {"err":"Expected name 'Angela', but instead found: "+ value} |
| 27 | + |
| 28 | + # Delete the row from TestTable |
| 29 | + cmd = "DELETE FROM TESTTABLE WHERE name='Angela'" |
| 30 | + result = ibm_db.exec_immediate(conn, cmd) |
| 31 | + if not result: |
| 32 | + return {"err":"error :"+cmd} |
| 33 | + |
| 34 | + # If no detected errors occurred so far; return Success status |
4 | 35 | return {"message":"Successfully called db2 action"} |
0 commit comments