'Read a "." separated file and create a SQL query using python [closed]

Input file:

sch1.ft1
sch1.ft2
sch2.ft3
sch2.ft4

Query:

def get_tables_columns(filename):
    res_dct = {}
    with open(filename) as f:
        for line in f:
            line = line.strip()
            cols = line.split('.')
            res_dct = {cols[i]: cols[i + 1] for i in range(0, len(cols), 2)}
    return res_dct

tables = get_tables_columns("SchemaTable.txt")
print(tables)

def get_sql(schema, table):
    statement = "select colno, colname, typename, length from syscat.columns where tabschema ='{0}' and tabname ='{1}' order by colno ".format(schema,table)
    return statement

Output from the function I wrote: {'sch2': 'ft4'}

I need to build a query using key and value like {sch1:fact1, sch1:fact2, sch2:fact3, sch2:fact4}.

Desired SQL:

the requirement is if the sql runs for 
`select tabschema, tabname, colno, colname, typename, length from syscat.columns where tabschema ='sch1' and tabname ='ft1' order by colno`
the output should be stored in sch_ft1.txt. like wise for all the contents in input file.

is it possible to store the output of each sql in diff file. for example if the code is running for select colno, colname, typename, length from syscat.columns where tabschema ='sch2' and tabname ='fact4' order by colno i need the contents to store in sch2_fact4.txt file like wise for rest of the queries



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source