zanbri
zanbri

Reputation: 6006

Switching between Python dictionaries and SQLite

I have written python code which involves the use of dictionaries. I now wish to store my processed data onto a local SQLite database, but can't think of how to translate something like:

myDict[wordA] = [list of tuples (length changes per word)]

into SQL. I have changed myDict to be an SQL table with word being the first element, but I don't know how to assign columns to a variable list.

Do I have to change the structure of the DB?

Any ideas would be great!

Upvotes: 1

Views: 527

Answers (1)

Niek de Klein
Niek de Klein

Reputation: 8834

I would suggest you follow through a database tutorial like: http://www.quackit.com/database/tutorial/.

You'll want to make two tables, one that contains the word values, and one that contains the tuples. The tuples should not be in a list, but all have a separate row. You can link the tuples to the words using foreign keys. You can link multiple rows to the same word, in that way keeping the tuples from the list linked to a single word.

It would look like this, the foreign_key of the tuple table linking to the primary_key of the Word table:

Word table                              tuple table

primary_key   word                      primary_key     tuple     foreign_key
1             example1                  1               (1,2)     1
2             example2                  2               (3,9)     1
                                        3               (4,1)     2
                                        4               (4,2)     2

This would correspond to

myDict = {'example1' = [(1,2), (3,9)], 'example2' = [(4,1), (4,2)]}

Upvotes: 1

Related Questions