简体   繁体   中英

Inserting dictionary into sqlite3

In my project I need to count where each of the urls stored in database was accessed from.

For example:

example.com/1 - {'UK': 3, 'Germany': 15}
example.com/2 - {'Italy': 7, 'USA': 4}

I'm currently using SQLite3 and SQLAlchemy.

Is there any way to save the data from the dictionaries in the database? Or is it possible to achieve this in a better way?

The only thing that comes to my mind is to save the data in JSON converted to string.

It is a simple database design problem.

I would recommend:

CREATE TABLE country_clicks (
   id INTEGER PRIMARY KEY,
   country STRING,
   accessed_from STRING,
   clicks INTEGER
);
CREATE UNIQUE INDEX country_clicks_idx ON country_clicks(country, accessed_from);

Normally, I would recommend country and accessed_from as composite PRIMARY KEY. But SQLite had no such thing from the start (there is some mechanics to do so in newer versions).

You have to make sure, that when you insert new values in the table, that the combination country/accessed_from does not already exist. If so, just update clicks.

Using a composite primary key:

CREATE TABLE country_clicks(
     url TEXT NOT NULL
   , country TEXT NOT NULL
   , clicks INTEGER
   , PRIMARY KEY(url, country)
   ) WITHOUT ROWID

Side note: If you just have a list of (url, country) pairs and need to count how many times each unique combination shows up in the list, sqlite can do it for you. Change the clicks column in the above to clicks INTEGER NOT NULL DEFAULT 1 , and then insert via:

INSERT INTO country_clicks(url, country)
       VALUES (?, ?)
       ON CONFLICT (url, country) DO UPDATE SET clicks = clicks + 1

(Note: This requires Sqlite 3.24 or better for the upsert support).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM