I use the following python3 code to add and update entries in an sqlite3 database:
def increment_person_counts(count_per_person):
with sqlite3.connect(r'./people_database') as connection:
cursor = connection.cursor()
for person in count_per_person:
if cursor.execute('select * from personCounts where person = ?', [person]).fetchone()==None:
cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count_per_person[person]])
else:
cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count_per_person[person], person])
connection.commit()
count_per_person contains 4 million entries, and I seem to be able to add/update around 100 entries per second which means it will take half a day to add these values. Is there a better/faster approach to doing this that I should consider?
Thanks for your help,
Barry
You can read your whole
'select * from personCounts'into a pythonset()at the beginning and then check just against this set.UPDATE: after my comment, here is the update with
executemany: