情景:
list
值,其中包含400个元素,即每个list
值的元素5将位于第5行。我需要将这些词典中的数据插入到上述表格中,同时考虑到某些词典不会使用表格中的所有14列。
最有效的方法是什么?
答案 0 :(得分:1)
假设您的数据已经是sql-safe并且每个dict的键都足以插入一行:
import sqlite3
from itertools import izip, repeat
conn = sqlite3.connect('mydb.sqlite3')
table_name = 'test'
cursor = conn.cursor()
# Create test table.
cursor.execute('DROP TABLE IF EXISTS {}'.format(table_name));
cursor.execute('CREATE TABLE {} (a1 integer, a2 integer, a3 integer)'.format(table_name));
conn.commit()
my_dicts = [
{ 'a1': repeat(1, 5), 'a2': repeat(2, 5), 'a3': repeat(3, 5) },
{ 'a2': repeat(4, 2) },
{ 'a3': repeat(7, 7) },
]
for my_dict in my_dicts:
# Key <=> Items order has to be preserved.
# .keys() and .values() not used to be sure
# the order is preserved.
keys = []
items = []
for k, v in my_dict.iteritems():
keys.append(k)
items.append(v)
sql = "INSERT INTO {} ({}) VALUES ({})".format(table_name, ','.join(keys), ','.join(repeat('?', len(keys))))
cursor.executemany(sql, izip(*items))
conn.commit()
cursor.execute('SELECT * FROM {}'.format(table_name));
conn.commit()
>>> print cursor.fetchall()
[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (None, 4, None), (None, 4, None), (None, None, 7), (None, None, 7), (None, None, 7), (None, None, 7), (None, None, 7), (None, None, 7), (None, None, 7)]