以下均使用sqlite3数据库测试
先来看看原生SQL语句批量插入的情况
下面是插入10万条数据的对比
import time
import sqlite3
def read_data():
with open("fake_data", "r") as f:
data = f.readlines()
# data = map(lambda i: i.strip(), data) #execute使用的data
data = [(i.strip(),) for i in data] #executemany使用的data
return data
def test_sqlite3_execute(data, cur):
for d in data:
sql = 'insert into person(name) values("%s")' % d
cur.execute(sql)
def test_sqlite3_executemany(data,cur):
sql = 'insert into person(name) values(?)'
cur.executemany(sql,data)
if __name__ == '__main__':
connect = sqlite3.connect("test.db")
cur = connect.cursor()
data = read_data()
start = time.time()
test_sqlite3_executemany(data, cur)
# test_sqlite3_execute(data,cur)
connect.commit()
end = time.time()