前端之家收集整理的这篇文章主要介绍了
SQLite Transactions,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "../sqlite3/sqlite3.h"
#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
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,name nvarchar(32),sex integer,age integer)",NULL,&errmsg ); /* primary key*/
if(result !=sqlITE_OK)
printf("创建表失败,错误码:%d,错误原因:%s\n",result,errmsg );
#ifdef USE_TRANSACTION
result = sqlite3_exec( db,"begin;",&errmsg );
#endif
char ssql[128] = {0};
for (int i=0;i<TEST_TIMES;i++)
{
memset(ssql,128);
sprintf(ssql,"insert into Info values (%d,'%s',%d)",i,Name[rand()%4],Sex[rand()%2],rand()%50+1);
result = sqlite3_exec( db,ssql,&errmsg );
if(result !=sqlITE_OK)
printf("插入记录失败,错误码:%d,错误原因:%s\n",errmsg );
}
#ifdef USE_TRANSACTION
result = sqlite3_exec( db,"commit;",&errmsg );
#endif
#else
result = sqlite3_open("test.db","select * from Info",SleectCB,&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;
}
原文链接:https://www.f2er.com/sqlite/202339.html