我正在處理超過2900萬個元素,因此認爲數據庫比數組更有意義。將列表序列化爲SQLite
以前我一次只傳遞一個元素到execute
函數,但我相信每次向executemany
函數傳遞100,000個元素的數組會更有效。
我縮短了我的180奇數行代碼到這個簡短的測試案例:
import sqlite3
if __name__ == '__main__':
connection = sqlite3.connect('array.db')
cursor = connection.cursor()
cursor.execute("create table array (word text);")
cursor.executemany("insert into array values (?)", [u'usa', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'los', u'angeles'])
connection.commit()
cursor.execute("select * from array;")
print cursor.fetchall()
輸出:
Traceback (most recent call last):
cursor.executemany("insert into array values (?)", [u'usa', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'los', u'angeles'])
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
我在做什麼錯?