How do I create a table in sqlite3 with dictionary items as fields?
I can't simply define each and every field manually, since I have a lot of dictionaries of various sizes. Is there a way to accomplish this?
Here's what I've come up so far:
import sqlite3
DICT = { 'field 1': 'TEXT',
'field 2': 'TEXT',
'field 3': 'INT',
'field 4': 'TEXT'
'field x': '???'
}
def create_table(DICT):
with sqlite3.connect("data.db") as connection:
cursor = connection.cursor()
# should instead iterate over the dictionary and create a field for each entry
cursor.execute("""CREATE TABLE table_name
(dict_key_x, DICT_VALUE_X)""")
connection.close()
Any ideas are welcome. Thanks! :)