python - 在 SQLite 数据库中输入大量条目

标签 python sqlite python-3.x

我使用以下 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/

相关文章:

python - 笛卡尔积 - 使用 BackTracking - Python

python - 如何动态更改 __slots__ 属性?

python - 循环遍历 SQL 中的表并每次更新一行

python - Matplotlib 热图 : Image rotated when heatmap plot over it

python - 为什么 scipy.sparse.linalg.eigsh 给出错误的答案?

android - 如何使用 SQL 查找特定时间范围内的条目总数?

php - 为 Android 应用程序存储数据的最佳方法?

python - 根据元素在列表中出现的次数打印输出

java - FTS3 | rowid 问题 |删除不起作用

python - python 3中字符串切片操作的时间复杂度