I'm doing some batch imports of song data into a sqlite3 database. The raw data contains duplicates (and lots of them), so upon import, I'd like to be able to add a row if the song/artist is not in the database, otherwise increment a play_count
column.
Is there a way to do this with a single SQLite statement so that I can use sqlite3
's executemany()
function, as opposed to the less efficient method of looping over the data set and doing multiple execute()
statements?
For example, if I have the following raw song data:
[('This Song', 'This Artist'), ('Different Song', 'Different Artist')]
Is there a way to add it to the following table if it doesn't already exist, and increment play_count
if it does?
song_title | artist | play_count
------------- | -------------- | ----------
This Song | This Artist | 1
That Song | That Artist | 3
Another Song | Another Artist | 4
The raw data set is very large, and will be done in batches, so I can't do the duplicate handling on the python side.