【发布时间】:2012-01-25 22:46:59
【问题描述】:
我使用以下 python3 代码在 sqlite3 数据库中添加和更新条目:
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 包含 400 万个条目,我似乎能够每秒添加/更新大约 100 个条目,这意味着添加这些值需要半天时间。有没有我应该考虑的更好/更快的方法来做到这一点?
感谢您的帮助,
巴里
【问题讨论】:
标签: python sqlite python-3.x