[英]Can python sqlalchemy use a variable to construct an insert statement (for mysql database)
在此先感謝您提供有關此問題的建議...
我正在嘗試創建一個 python 腳本來將一組 CSV 導入到 mysql 數據庫中。
每個 CSV 文件名都與目標表相匹配。 每個 CSV 的第一行與表的字段匹配。 每個 CSV / 表都有不同數量的字段、字段名稱等。
我遇到的問題是這一行(完整代碼如下)
ins = table_name.insert().values(temp_variable_name)
我想動態更新目標表 (table_name) 和插入命令 (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語句的插入命令?
下面的腳本部分:
# 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)
您可以使用Insert
對象和csv
模塊執行此操作,這使得使用DictReader
類變得容易。 以下是 company 表的示例:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.