简体   繁体   English

如何在列表中插入列表列表? [蟒蛇]

[英]How to insert a list of lists into a table? [Python]

I have this list with column names: 我有带有列名的列表:

csvfields = ['id', 'gen', 'age', 'mar', 'loc', 'inc', 'iscr', 'escr']

And data list with lists: 以及带有列表的数据列表:

csvdata = [['51', 'F', '46', 'M', '0', '15100', '531', '555'], 
['52', 'M', '29', 'M', '2', '14200', '673', '633'], 
['53', 'M', '25', 'S', '0', '22200', '742', '998'], 
['54', 'M', '36', 'M', '2', '1000', '677', '646'], 
['55', 'F', '99', 'S', '0', '10600', '608', '998'], 
['56', 'F', '45', 'M', '2', '6100', '710', '743'], 
['57', 'M', '99', 'M', '2', '16500', '679', '646'], 
['58', 'F', '37', 'M', '0', '7400', '637', '683'], 
['59', 'M', '45', 'S', '0', '22800', '683', '998'], 
['60', 'M', '22', 'S', '0', '6400', '699', '998']]

I created a function that creates a table with the csvfields al columns: 我创建了一个函数,该函数使用csvfields al列创建一个表:

def create_dbtb(dbname, tablename):
    conn = sqlite3.connect(dbname)
    curs = conn.cursor()
    columns =  ', '.join(csvfields)
    curs.execute('''CREATE TABLE IF NOT EXISTS {}({})'''.format(tablename, columns))
    conn.commit()
    conn.close()

But now I want to create another function that inserts the data in csvdata into the created table. 但是现在我想创建另一个函数,将csvdata的数据插入到创建的表中。 I created the next function but it's just not working and I don't know how to fix this. 我创建了下一个函数,但是它不起作用,我也不知道如何解决此问题。

def fill_dbtb(self):
    conn = sqlite3.connect('data.db')
    curs = conn.cursor()
    columns = ', '.join(csvfields)
    data = ', '.join([row for row in csvdata])
    curs.execute('''INSERT INTO data {} VALUES ({})'''.format(columns, data)) 

Anyone who knows how to fix this? 有谁知道如何解决这个问题?

BTW, this is the error I get after running the second function: 顺便说一句,这是我运行第二个函数后得到的错误:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    x.fill_dbtb()
  File "/Users/dirkkoomen/Desktop/Python cursus/inzendopgave 4/opgave 6/oefnen.py", line 48, in fill_dbtb
    curs.execute('''INSERT INTO data ({}) VALUES ({})'''.format(', '.join(self.csvfields), ', '.join([row for row in self.csvdata])))
TypeError: sequence item 0: expected str instance, list found

Finally found a way to fix it, don't know if it's the best way? 终于找到了解决它的方法,不知道这是不是最好的方法?

def create_dbtb(dbname, tablename):
    conn = sqlite3.connect(dbname)
    curs = conn.cursor()
    columns =  ', '.join(csvfields)
    curs.execute('''DROP TABLE IF EXISTS {}'''.format(tablename))
    curs.execute('''CREATE TABLE {}({})'''.format(tablename, columns))
    conn.commit()
    conn.close()

def fill_dbtb():
    conn = sqlite3.connect('data.db')
    curs = conn.cursor()
    csvdata_new = [tuple(l) for l in csvdata]
    fields = tuple(csvfields)
    for item in csvdata_new:
        var_string = ', '.join('?'*len(item))
        curs.executemany('''INSERT INTO data {} VALUES ({})'''.format(fields, var_string), (item,))
    conn.commit()
    conn.close()

This works fine, let me know if there's a better way. 效果很好,让我知道是否有更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM