How to insert several thousand columns in sqlite3?

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}
    }

# 1. Find the unique column names.
columns = set()
for _, cols in dic.items():
    for key, _ in cols.items():
       columns.add(key)

# 2. Create the schema.
col_defs = [
    # Start with the column for our key name
    '"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)

# 3. Loop through each row
for row_name, cols in dic.items():

    # Compile the data we have for this row.
    col_names = cols.keys()
    col_values = [str(val) for val in cols.values()]

    # Insert it.
    sql = 'INSERT INTO simple ("row_name", "%s") VALUES ("%s", "%s");' % (
        '","'.join(col_names),
        row_name,
        '","'.join(col_values)
        )
+3
source share
2 answers

, , . SQLite ( 2000), , SQLite. , , Python, .

, . - /OLAP (, ), SQLite / OLAP. - - ( , , , SQLite).

+6

, , .

COMMIT (commit()) ( ), .

sqlite. , - . , .

0

Source: https://habr.com/ru/post/1742563/


All Articles