在编译后当然就是使用sqlite,贸贸然去测试了一下创建数据库,插入数据,仅几条数据,发现,真不错的数据库,后来把数据量提高到10000的 时候,发现,怎么这么慢,后来自己都受不了,网上一查,原来是如果在没有使用事务的情况下每次执行都会自动代表一次事务进行数据库读写,而不是在内存中操 作,想想看这样必然很慢,后来自己把事务添加上去,真的很惊讶,提高数度肯定有好几千倍。兴奋。。。。
把代码帖一下:
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <MMSystem.h>
#pragma comment(lib,"winmm.lib")
#define TEST_TIMES 10000
#define ONE_M 1048576
#define TEST_SELECT
#define USE_TRANSACTION
#include "sqlite3.h"
typedef int(*sqlite3_callback)(void*,int,char**,char**);
char Name[4][32] = {"david","boluns","guanzhongdaoke","wangba"};
char Sex[2][8] = {"boy","girl"};
int SleectCB(void* para,int n_column,char** column_value,char** column_name )
{
printf("记录包含%d个字段\n", n_column );
for(int i = 0 ; i < n_column; i ++ )
{
printf("字段名:%s 字段值:%s\n", column_name[i], column_value[i] );
}
printf("------------------\n");
return 0;
}
int sqlite_main()
{
sqlite3 * db = 0;
int result = 0;
char * errmsg = 0;
#ifdef TEST_SELECT
remove("test.db");
result = sqlite3_open("test.db", &db );
if( result !=SQLITE_OK)
return -1;
result = sqlite3_exec( db,"create table Info( ID integer primary key
, name nvarchar(32) ,sex integer,age integer)", NULL, NULL, &errmsg
);
if(result !=SQLITE_OK)
printf("创建表失败,错误码:%d,错误原因:%s\n", result, errmsg );
#ifdef USE_TRANSACTION
result = sqlite3_exec( db,"begin;", 0, 0, &errmsg );
#endif
char sSQL[128] = {0};
for (int i=0;i<TEST_TIMES;i++)
{
memset(sSQL,0,128);
sprintf(sSQL,"insert into Info values (%d,'%s','%s',%d)",i,Name[rand()%4],Sex[rand()%2],rand()%50+1);
result = sqlite3_exec( db,sSQL, 0, 0, &errmsg );
if(result !=SQLITE_OK)
printf("插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg );
}
#ifdef USE_TRANSACTION
result = sqlite3_exec( db,"commit;", 0, 0, &errmsg );
#endif
#else
result = sqlite3_open("test.db", &db );
if( result !=SQLITE_OK)
return -1;
result = sqlite3_exec( db,"select * from Info", SleectCB, NULL, &errmsg );
#endif
sqlite3_close( db );
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwNow = timeGetTime();
sqlite_main();
printf("数据库初始化用时:%dms\r\n",timeGetTime()-dwNow);
return 0;