python sqlalchemy可以使用变量来构造一个insert语句(对于mysql数据库)

时间:2015-02-08 23:09:23

标签: python mysql csv

提前感谢您就此问题提出建议......

我正在尝试创建一个python脚本,将一组CSV导入到mysql数据库中。

每个CSV文件名与目标表匹配。每个CSV的第一行与表的字段匹配。每个CSV /表都有不同数量的字段,字段名称等。

我遇到的问题是这一行(下面的完整代码)

ins = table_name.insert().values(temp_variable_name)

我想动态更新目标表(table_name)和insert命令(temp_variable_name)。

因此,在阅读labels.csv文件时,应生成

ins = labels.insert().values(id_label=d[0], label_name=d[1])

在阅读company.csv文件时,应该生成

ins = company.insert().values(id_company=d[0], company_name=d[1], ticker=d[2])

问题是如果我生成一个字符串,

temp_variable_name = 'id_company=d[0], company_name=d[1], ticker=d[2]'

我最终得到了一个' str'对象没有属性' items'错误。

有没有办法动态生成SQL语句的insert命令?

以下脚本的部分:

# files list contains a list of all of the files in the directory
# we read in CSVs, isolate the first row to determine table field names
# the rest of the data should then be imported into the table with the corresponding name as the CSV

for f in files:
    if '.csv' in f :

        # read in each CSV file

        # these are a Class / Function I've set up to read files
        x = Read_Files()  
        data = x.read_file_lines_strip(path, f)

        temp = data[0].replace('"','') # get rid of quotation marks from the data

        table_header_list = temp.split('|') # get the first row, which is the table field names

        variable_name ='' # this is used to construct the insert into table string

        for x in xrange (0, len(table_header_list)) :
            if x == 0 :
                variable_name = variable_name + table_header_list[0] + '=d[0]'
            elif x == len(table_header_list) :
                variable_name = variable_name + table_header_list[x] + '=d[' + str(x) + ']'
            else :
                variable_name = variable_name + ', ' + table_header_list[x] + '=d[' + str(x) + ']'

        table_name = f.replace('.csv','') # remove the .csv from filename to isolate the file name, which is the same as table name

        # data from file

        for data_line in data[1:] :
            data_line = data_line.replace('"', '') # remove quotation marks
            d = data_line.split('|') # split the line which is delimited by a |

            # used to construct the final insert string
            for x in xrange(0, len(table_header_list)) :
                if x == 0 :
                    temp_variable_name = variable_name.replace('d[0]', d[0])
                else :
                    temp_variable_name = temp_variable_name.replace('d[' + str(x) + ']', d[x])


            try:
                # table name is the table to insert into, via the CSV filename
                # temp_variable_name is the insert string, such as 'id_company=d[0], company_name=d[1], ticker=d[2]'
                ins = table_name.insert().values(temp_variable_name)
                result = conn.execute(ins)
            except Exception, e :
                print 'error : ' + str(e)

1 个答案:

答案 0 :(得分:0)

您可以使用Insert个对象和csv模块执行此操作,这样可以轻松使用DictReader类。这是公司表的一个例子:

import csv
from sqlalchemy import create_engine
from sqlalchemy.sql import table, column

NULL_FIELD_VALUE = r'\N'
DB_CONNECT = 'sqlite:///company.db'
engine = create_engine(DB_CONNECT, echo=True)
conn = engine.connect()

with open('company.csv') as csvfile:
    reader = csv.DictReader(csvfile, delimiter='|')
    insert_table = table('company',
                         *[column(field) for field in reader.fieldnames])
    insert_dict = [{k: None if v == NULL_FIELD_VALUE else v
                        for k,v in row.items()}
                            for row in reader]
    conn.execute(insert_table.insert(), insert_dict)