mardi 17 mars 2015

Inserting list of lists in database, using dict with column names

I want to insert a lot of lists into a sqlite table. I have a dict that tells me which position on the list represents which column name, like so:



data = [ ['a', 'b', 'c'], ['a', 'b', 'c'] ]
column_names = {0: 'a_column', 1: 'b_column', 2: 'c_column'}


I would like to insert this data into an sqlite table, where the keys of the column_names dict represent the indexes of the values in data and the values in column_names are the names of the columns they should be inserted into. I'm looking for something like the following, in pseudocode:



sqlit3.magic('INSERT INTO mytable (column_names.values()) VALUES (i.magicsort(column_names.keys()) for i in data)')

Aucun commentaire:

Enregistrer un commentaire