Code:
-
-
- import ibm_db_dbi as db2
- import ibm_db
- import sys
- import os
- import time
-
-
- schema = sys.argv[1]
- table = sys.argv[2]
- qualified_table = schema + "." + table
-
-
- output_file = "SQL_SELECT.txt"
-
- output_path = "/home/buck/"
- full_file = output_path + output_file
-
- outfile = open(full_file, "w")
-
- dsn = "Database=*LOCAL"
- host = "N/A"
- conn_options = {ibm_db.SQL_ATTR_AUTOCOMMIT: ibm_db.SQL_AUTOCOMMIT_OFF}
- connection = db2.connect(dsn, "", "", host)
- cursor = connection.cursor()
-
- sql_stmt = """\
- select table_name, column_name
- from qsys2.syscolumns
- where table_name = '""" + table + """'
- and table_schema = '""" + schema + """'
- order by ordinal_position;
- """
-
- result = cursor.execute(sql_stmt)
-
- if result == False:
- print('SQL error!')
- print('error ' + db2.stmt_error())
- print('errormsg ' + db2.stmt_errormsg())
- print('messages ' + cursor.messages())
-
- outfile.write("select \n")
-
- for row , row_value in enumerate(cursor, start=1):
-
-
-
-
- if (row > 1):
- outfile.write(",\n")
-
- qualified_column_name = row_value[0] + '.' + row_value[1]
-
-
-
- outfile.write(row_value[1])
-
- outfile.write("\nFROM " + qualified_table + ";\n")
-
- connection.close()
-
- outfile.close()
|
|