我有OrderedDicts
的键顺序列表正确,每个词典中有72个键,如何将它们写到SQL表中,
from collections import OrderedDict
import sqlite3
from sqlite3 import OperationalError
#code here
#my_list=list of OrderedDicts
conn = sqlite3.connect('test3.db')
cursor = conn.cursor()
我检查了Insert a list of dictionaries into an SQL table using python,
cursor.executemany("""
INSERT INTO
mytable
(id, price, type)
VALUES
(%(key1)s, %(key2)s, %(key3)s, ...(key72))
""", lst) # there must be a more pythonic way of performing this.
我想要的是将my_list
写入表的方式。
答案 0 :(得分:0)
您可以使用熊猫直接书写,请参考pandas.DataFrame.to_sql文档。
import pandas
#your code
conn = sqlite3.connect('test3.db')
cursor = conn.cursor()
df=pandas.DataFrame(my_list) # list of dictionaries
df.to_sql("name_of_table",conn)
conn.close()