2011-12-22 54 views
3

我用下面的python3代碼在一個sqlite3的數據庫中添加和更新條目大量條目:進入一個SQLite數據庫

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中包含4萬個條目,我似乎能夠添加/每秒更新大約100個條目,這意味着需要半天才能添加這些值。有沒有更好/更快的方法來做到這一點,我應該考慮?

感謝您的幫助,

巴里

回答

2

你可以閱讀你的整個'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() 

UPDATE:我的意見後,這裏是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() 
+0

沒錯,好主意! – Baz 2011-12-22 09:07:14

+0

@Baz - 你也可以在插入時試用'executemany'方法:http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.executemany – eumiro 2011-12-22 09:09:20

+0

是的,特別是在我插入新條目之後以及將現有條目更新爲單獨的python函數,分別建立了需要更新或添加的內容。 – Baz 2011-12-22 09:14:56

相關問題