python sqlite3插入测试代码,默认情况下需要提交事务的,如果要设置con.isolation_level = None则每次自动提交事务:
当插入1000条数据时,自己提交事务用了,0.11s;程序自动提交事务用了1分半钟。
- #!/usr/bin/python
- #-*- coding: utf-8 -*-
- import sqlite3, datetime
- def insertData(n):
- for i in range(n):
- cur.execute('INSERT INTO foo (o_id, fruit, veges) VALUES(NULL, "apple", "broccoli")')
- con.commit()
-
- def deleteData():
- cur.execute('delete from foo')
- print 'delete over!'
- con.commit()
-
- def createTable():
- cur.execute('CREATE TABLE foo (o_id INTEGER PRIMARY KEY, fruit VARCHAR(20), veges VARCHAR(30))')
- con = sqlite3.connect("my.db")
- #这个就是事务隔离级别,默认是需要自己commit才能修改数据库,置为None则自动每次修改都提交
- #con.isolation_level = None
- cur = con.cursor()
- insert_begin = datetime.datetime.now()
- #创建表格
- #createTable()
- #插入数据
- insertData(1000000)
- #deleteData()
- insert_end = datetime.datetime.now()
- print "insert data spend time :%s"%(insert_end-insert_begin)
- print 'last id: %s'%cur.lastrowid
阅读(4728) | 评论(0) | 转发(0) |