Answer the question
In order to leave comments, you need to log in
How to write only unique rows in SQLite?
Good afternoon!
There was a question, I just can’t figure it out, the task is the following: I collect data from a certain site through BeautifulSoup about upcoming sporting events in the format:
Football - 08/08 - 19:00 - Team 1 - Team 2, Champions League
Tennis - 08/08 - 18:00 - Player 1 - Player 2, WTA
Then all this is recorded in the SQLite database. Parsing runs once every 30 minutes, and it is necessary to write only unique values to strings, that is, when you restart parsing, you want to make sure that repeated values are simply ignored.
If I understand correctly, when using INSERT OR IGNORE, even if the value in one column matches, the row will not be written. But what if, for example, there is a match in a week:
Football - 15/08 - 19:00 - Team 1 - Team 2, Champions League
It turns out that everything will be the same, except for the date, it turns out that you need to check immediately, for example, the date, time, team name.
I wanted to make a SELECT query when writing to the database, and if there is no such unique row, do INSERT, but I'm afraid that when the table grows, there will be brakes when writing? Or let the script write everything, and then use a separate script to check the values for uniqueness once a day and delete unnecessary ones?
Is there any "standard" solution, or what is the better way to go?
Answer the question
In order to leave comments, you need to log in
what is the structure of the table?
That's right, you need to use INSERT OR IGNORE, but define PRIMARY KEY, in this case based on several fields, and then a record will be added in a week, since the play_date field has a different value.
CREATE TABLE IF NOT EXISTS games
(game TEXT NOT NULL, play_date TEXT NOT NULL, play_time TEXT NOT NULL,
team_a TEXT NOT NULL, team_b TEXT NOT NULL, cup TEXT,
PRIMARY KEY (game , play_date , play_time ));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question