// testsqlite3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "testsqlite3.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include <io.h>
#include "..\\SQLite3\\sqlite3.h"
#pragma comment(lib, "SQLite3.lib")

#include "StringProcess.h"
// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

#define __USE_KEY__

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
	int i;
	for(i=0; i<argc; i++)
	{
		printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
	}
	printf("\n");
	return 0;
}

#define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK) \
{printf("%s error!\n",szInfo);\
printf("%s\n",szErrMsg);    \
sqlite3_free(szErrMsg);         \
sqlite3_close(db);              \
	system("pause");\
return 0;}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: 在此处为应用程序的行为编写代码。
		sqlite3 *db;
		char *dbPath="test.db";
		char *szErrMsg = 0;

		char szpath[MAX_PATH] = {0};

		std::string ss= "test.db";
		if ( StringProcess::ascii_to_utf8(dbPath, szpath) == 0)
			return 0;
		StringProcess::GB2312ToUTF_8(ss, dbPath, strlen(dbPath));
			
		char *szSql = NULL;
		sprintf(szpath, _T("%s"), ss.c_str());
		if (  -1 == _access(dbPath, 0 ) )
		{
			int rc= sqlite3_open(szpath, &db);
#ifdef __USE_KEY__
			if ( rc == SQLITE_OK )
			{
				// rekye,对未加密的数据加密;
				//if ( SQLITE_OK != sqlite3_rekey(db, "0123456789abcdef", 16))
				if ( SQLITE_OK != sqlite3_key(db, "0123456789abcdef", 16))
				{
					//szErrMsg = "sqlite encrypt error:";
					CHECK_RC(rc,"sqlite encrypt error:",szErrMsg,db);	
				}
			}
#endif
			rc=sqlite3_exec(db,"PRAGMA synchronous = OFF",0,0,0);    //提高性能
			rc=sqlite3_exec(db,"PRAGMA cache_size = 8000",0,0,0); //加大缓存
			rc=sqlite3_exec(db,"PRAGMA count_changes = 1",0,0,0); //返回改变记录数
			rc=sqlite3_exec(db,"PRAGMA case_sensitive_like = 1",0,0,0); //支持中文LIKE查询


			CHECK_RC(rc,"open database",szErrMsg,db);
			szSql="create table UserInfo(ID int primary key , UserName char, PassWord char);";
			rc=sqlite3_exec(db,szSql,0,0,&szErrMsg);
			CHECK_RC(rc,"create table",szErrMsg,db);
			rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(1,'kfqcome','123456')",0,0,&szErrMsg);
			CHECK_RC(rc,"insert info",szErrMsg,db);
			rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(2,'miss wang','654321')",0,0,&szErrMsg);
			CHECK_RC(rc,"insert info",szErrMsg,db);
			szSql="select * from UserInfo";
			rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
			CHECK_RC(rc,"query values",szErrMsg,db);
			sqlite3_close(db);
		}
		else
		{
			int rc= sqlite3_open(szpath, &db);
			CHECK_RC(rc,"open database",szErrMsg,db);
#ifdef __USE_KEY__
			if ( SQLITE_OK != sqlite3_key(db, "0123456789abcdef", 16))
			{
				//szErrMsg = "sqlite encrypt error:";
				CHECK_RC(rc,"sqlite encrypt error:",szErrMsg,db);	
			}
#endif
			char *szSql="select * from UserInfo";
			rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
			CHECK_RC(rc,"query values",szErrMsg,db);
			sqlite3_close(db);
		}
		
		//getchar();
		system("pause");
	}

	return nRetCode;
}
