我使用以下 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 个条目,这意味着添加这些值需要半天时间。有没有我应该考虑的更好/更快的方法?
谢谢你的帮助,
巴里
最佳答案
您可以在开始时将整个 'select * from personCounts'
读入 python set()
,然后只检查这个集合。
def increment_person_counts(count_per_person):
with sqlite3.connect(r'./people_database') as connection:
cursor = connection.cursor()
cursor.execute('select person from personCounts')
known_persons = set(row[0] for row in cursor.fetchall())
for person, count in count_per_person.iteritems():
if person in known_persons:
cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count])
else:
cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
connection.commit()
更新:在我的评论之后,这里是executemany
的更新:
def increment_person_counts(count_per_person):
with sqlite3.connect(r'./people_database') as connection:
cursor = connection.cursor()
cursor.execute('select person from personCounts')
known_persons = set(row[0] for row in cursor.fetchall())
cursor.executemany('insert into personCounts(person, count) values (?, ?)', ((person, count) for count_per_person.iteritems() if person in known_persons))
for person, count in count_per_person.iteritems():
if person not in known_persons:
cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
connection.commit()
关于python - 在 SQLite 数据库中输入大量条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601414/