/**********打开数据库************/
main()
{
rc = sqlite3_open("entrance_guard.db",&db_recode_sqlite);
if(rc)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_recode_sqlite));
printf("connot open database\n");
sqlite3_close(db_recode_sqlite);
exit(-1);
}
sqlite3_exec(db_recode_sqlite,"begin transaction",0,0,&errmsg); //开始一个事务
for(i=0; i<5000; i++)
{
sprintf(buf,"INSERT INTO \"MJ_Brush_Tab\" VALUES(%d,%d,%d,%d,%d,%d,%d);",i+1,i+1,0,0,0,0,0);
sql=buf;
sqlite3_exec( db_recode_sqlite, sql,0 ,0, &errmesg );
if(errmesg!=NULL)
{
printf("err:%s\n", errmesg);
sqlite3_free(errmesg);
return 0;
}
}
sqlite3_exec(db_recode_sqlite,"commit transaction",0,0,&errmsg); //提交一个事务
sqlite3_close(db_recode_sqlite);
}
使用事务的方法以后从原来插5000记录需要几十分钟缩减到只有22秒,大大提高了连续插入的速度^_^
阅读(2963) | 评论(1) | 转发(0) |