我正在处理超过 2900 万个元素,因此认为数据库比数组更有意义。
以前我一次将一个元素传递给execute
函数,但我相信一次将一个包含 100,000 个元素的数组传递给executemany
函数会更有效。
我已将我的 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.
我究竟做错了什么?