使用CSV输入从Python运行动态查询

使用CSV输入从Python运行动态查询,python,sql,python-3.x,oracle,Python,Sql,Python 3.x,Oracle,我有一个CSV文件,其中包含以下格式的表名和这些表的主键: | Table Name | Primary Key | | Table 1 | Col1 | | Table 1 | Col2 | | Table 1 | Col3 | | Table 2 | Col11 | | Table 2 | Col12 | 但我在这个文件里有几千张表。如何动

我有一个CSV文件,其中包含以下格式的表名和这些表的主键:

| Table Name | Primary Key | | Table 1 | Col1 | | Table 1 | Col2 | | Table 1 | Col3 | | Table 2 | Col11 | | Table 2 | Col12 | 但我在这个文件里有几千张表。如何动态地编写和执行此查询并将结果写入平面文件? 我想用Python3来执行它

尝试:

CSV:

我的PKTest.py

def getColumns(filename):
    tables = {}

    with open(filename) as f:
        for line in f:
            line = line.strip()
            if 'Primary Key' in line:
                continue

            cols = line.split('|')
            table = cols[1].strip()
            col = cols[2].strip()

            if table in tables:
                tables[table].append(col)
            else:
                tables[table] = [col]
    return tables

def runSQL(table, columns):
    statement = 'select {0} from {1} group by {0} having count(*) > 1'.format(', '.join(columns), table.replace(' ',''))
    return statement

if __name__ == '__main__':
    tables = getColumuns('PKTest.csv')
    try:
        #cursor to connect

        for table in tables:
            sql = runSQL(table,tables[table])
            print(sql)
            cursor.execute(sql)
            for result in cursor:
                print(result)

    finally:
        cursor.close()
    ctx.close()

因为我没有访问Oracle的权限,所以您必须临时修改一下这个答案

假设有一个名为
so.csv
的文件,其中包含问题中显示的数据

像这样创建一个名为
so.py
的文件。我将添加一些代码和一些解释。您可以将文件拼凑在一起,或从此处复制/粘贴它:

在文件顶部,导入Oracle依赖项:

# import cx_Oracle
# https://www.oracle.com/technetwork/articles/dsl/python-091105.html
然后,创建一个函数来解析您的
so.csv
,并将表和列放入字典中,如下所示:
{'table 1':['Col1','Col2','Col3'],'table 2':['Col11','Col12']}

def get_tables_columns(filename):

    tables = {}

    with open(filename) as f:
        for line in f:
            line = line.strip()
            if 'Primary Key' in line:
                continue

            cols = line.split('|')

            table = cols[1].strip()
            col = cols[2].strip()

            if table in tables:
                tables[table].append(col)
            else:
                tables[table] = [col]

    return tables
然后,创建一个函数,如果它知道表和列列表,则生成sql:

def get_sql(table, columns):

    statement = 'select {0} from {1} group by {0} having count(*) > 1'.format(
            ', '.join(columns),
            table.replace(' ', '')
        )

    return statement
是时候执行这些功能了:

if __name__ == '__main__':
    tables = get_tables_columns('so.csv')

    # here goes your code to connect with Oracle
    # con = cx_Oracle.connect('pythonhol/welcome@127.0.0.1/orcl')
    # cur = con.cursor()

    for table in tables:
        sql = get_sql(table, tables[table])
        print(sql)

        # here goes your sql statement execution            
        # cur.execute(sql)
        # for result in cur:
        #    print result

    # close your Oracle connection
    # con.close()

您可以包含与Oracle相关的语句并运行python文件。

这看起来不错。我已经测试过了。把这个写进一个平面文件怎么样?追加?是的,您可以以当前形式执行它,如:
python3so.py>outputfile.sql
。这将为您提供outputfile.sql。我得到以下错误:
回溯(上次调用):文件“PKTest.py”,第28行,在tables=getColumuns('PK Test.csv')文件“PKTest.py”,第14行,在getColumuns table=cols[1]中。strip()索引器:列表索引超出范围
您还添加了
cols=line.split('.|'))
基于上表?如果我们正在阅读CSVYes,则不认为需要它,拆分是基于您示例中的
完成的。如果您有CSV,最好在您的问题中发布一个示例。您可以调整CSV的拆分。你的PKTest.py看起来像什么?您可能希望在编辑的问题中包含这一点,我很乐意提供帮助。
if __name__ == '__main__':
    tables = get_tables_columns('so.csv')

    # here goes your code to connect with Oracle
    # con = cx_Oracle.connect('pythonhol/welcome@127.0.0.1/orcl')
    # cur = con.cursor()

    for table in tables:
        sql = get_sql(table, tables[table])
        print(sql)

        # here goes your sql statement execution            
        # cur.execute(sql)
        # for result in cur:
        #    print result

    # close your Oracle connection
    # con.close()