新建一表, 就只有两列:
create table t(
id integer primary key autoincrement,
name text
);
1: 不使用事务, 向sqlite3里插入1万条数据, 大概需要22秒.
2: 使用事务, 向sqlite3里插入10万条数据(这里不是1万条, 而是10万条):
2.1:
QSqlDatabase db = DBUtil::getInstance().getDatabase();
db.transaction();
QSqlQuery query(db);
for (int i = 0; i < count; ++i) {
query.exec("INSERT INTO t(name) VALUES('biao')");
}
db.commit();
大概需要2900毫秒.
2.2:
QSqlDatabase db = DBUtil::getInstance().getDatabase();
db.transaction();
for (int i = 0; i < count; ++i) {
QSqlQuery query("INSERT INTO t(name) VALUES('biao')", db);
}
db.commit();
大概需要3200毫秒.
看来事务在大量数据操作的时候还是不错的.