|
| 1 | +# |
| 2 | +# Licensed Materials - Property of IBM |
| 3 | +# |
| 4 | +# (c) Copyright IBM Corp. 2025 |
| 5 | +# |
| 6 | + |
| 7 | +from __future__ import print_function |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | +import ibm_db |
| 11 | +import ibm_db_dbi |
| 12 | +import config |
| 13 | +from testfunctions import IbmDbTestFunctions |
| 14 | + |
| 15 | + |
| 16 | +class IbmDbTestCase(unittest.TestCase): |
| 17 | + |
| 18 | + def test_319_FetchMethods_DBI(self): |
| 19 | + obj = IbmDbTestFunctions() |
| 20 | + obj.assert_expect(self.run_test_319) |
| 21 | + |
| 22 | + def run_test_319(self): |
| 23 | + conn = ibm_db.connect(config.database, config.user, config.password) |
| 24 | + |
| 25 | + ibm_db.autocommit(conn, ibm_db.SQL_AUTOCOMMIT_OFF) |
| 26 | + |
| 27 | + dbi_conn = ibm_db_dbi.Connection(conn) |
| 28 | + |
| 29 | + # Drop the test table, in case it exists |
| 30 | + drop = 'DROP TABLE dbiEmployees' |
| 31 | + try: |
| 32 | + cur = dbi_conn.cursor() |
| 33 | + cur.execute(drop) |
| 34 | + except: |
| 35 | + pass |
| 36 | + |
| 37 | + # Create the test table |
| 38 | + create = 'CREATE TABLE dbiEmployees (id INT PRIMARY KEY NOT NULL, name VARCHAR(255), age INT)' |
| 39 | + cur = dbi_conn.cursor() |
| 40 | + cur.execute(create) |
| 41 | + |
| 42 | + # Insert rows into the table |
| 43 | + insert_sql = 'INSERT INTO dbiEmployees (id, name, age) VALUES (?, ?, ?)' |
| 44 | + employees = [ |
| 45 | + (1, 'Alice', 30), |
| 46 | + (2, 'Bob', 24), |
| 47 | + (3, 'Charlie', 29), |
| 48 | + (4, 'Diana', 35), |
| 49 | + (5, 'Eve', 40) |
| 50 | + ] |
| 51 | + for emp in employees: |
| 52 | + cur.execute(insert_sql, emp) |
| 53 | + |
| 54 | + dbi_conn.commit() |
| 55 | + |
| 56 | + # 1) fetchone example |
| 57 | + cur1 = dbi_conn.cursor() |
| 58 | + cur1.execute('SELECT * FROM dbiEmployees ORDER BY id') |
| 59 | + row = cur1.fetchone() |
| 60 | + print("fetchone():", row) |
| 61 | + |
| 62 | + # 2) fetchmany example |
| 63 | + cur2 = dbi_conn.cursor() |
| 64 | + cur2.execute('SELECT * FROM dbiEmployees ORDER BY id') |
| 65 | + rows = cur2.fetchmany(2) |
| 66 | + print("fetchmany(2):", rows) |
| 67 | + |
| 68 | + # 3) fetchall example |
| 69 | + cur3 = dbi_conn.cursor() |
| 70 | + cur3.execute('SELECT * FROM dbiEmployees ORDER BY id') |
| 71 | + all_rows = cur3.fetchall() |
| 72 | + print("fetchall():", all_rows) |
| 73 | + |
| 74 | +#__END__ |
| 75 | +#__LUW_EXPECTED__ |
| 76 | +#fetchone(): (1, 'Alice', 30) |
| 77 | +#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)] |
| 78 | +#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)] |
| 79 | +#__ZOS_EXPECTED__ |
| 80 | +#fetchone(): (1, 'Alice', 30) |
| 81 | +#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)] |
| 82 | +#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)] |
| 83 | +#__SYSTEMI_EXPECTED__ |
| 84 | +#fetchone(): (1, 'Alice', 30) |
| 85 | +#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)] |
| 86 | +#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)] |
| 87 | +#__IDS_EXPECTED__ |
| 88 | +#fetchone(): (1, 'Alice', 30) |
| 89 | +#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)] |
| 90 | +#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)] |
| 91 | + |
0 commit comments