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, NULL, &errmsg );  /* primary key*/

	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;
}

你可能感兴趣的:(sqlite,null,Integer,callback,include,transactions)