As in my last question, but I ran into a problem, I will say that I have a simple dictionary, for example below, but its large when I try to insert a large dictionary using the methods below. I am getting an operational error for c.execute (schema) for too many columns, and what should be my alternative method for populating the columns of SQL databases? Using alter table command and add each separately?
import sqlite3
con = sqlite3.connect('simple.db')
c = con.cursor()
dic = {
'x1':{'y1':1.0,'y2':0.0},
'x2':{'y1':0.0,'y2':2.0,'joe bla':1.5},
'x3':{'y2':2.0,'y3 45 etc':1.5}
}
columns = set()
for _, cols in dic.items():
for key, _ in cols.items():
columns.add(key)
col_defs = [
'"row_name" VARCHAR(2) NOT NULL PRIMARY KEY'
]
for column in columns:
col_defs.append('"%s" REAL NULL' % column)
schema = "CREATE TABLE simple (%s);" % ",".join(col_defs)
c.execute(schema)
for row_name, cols in dic.items():
col_names = cols.keys()
col_values = [str(val) for val in cols.values()]
sql = 'INSERT INTO simple ("row_name", "%s") VALUES ("%s", "%s");' % (
'","'.join(col_names),
row_name,
'","'.join(col_values)
)
source
share