// sqtr.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include "sqtr.h"
#include "sqlite3/sqlite3.h"
#include <windows.h>
#include <stdio.h>
#include <list>
#include <numeric>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <iostream>
#include "SQLiteWrapper.h"
using namespace std;
//#include <boost/lambda/lambda.hpp>
//#include <iterator>
//#include <boost/thread.hpp>
typedef list<char*> List_Char;
int db_fun(sqlite3 *pDB , List_Char *slist)
{
int result = 0;
//sqlite3 *pDB = NULL;
char* err_msg = NULL;
char* databaseName="test2.db3";
/*int rc = sqlite3_open(databaseName, &pDB);
sqlite3_close(pDB);*/
//int rc = sqlite3_open(databaseName, &pDB);
// 执行SQL
__int64 t1, t2, tc;
QueryPerformanceCounter((LARGE_INTEGER *)&t1);
int rc = sqlite3_exec(pDB, "BEGIN;", 0, 0, &err_msg);
//添加10条记录
for (List_Char::iterator s = slist->begin(); s!=slist->end(); s++)
{
// 执行SQL
char *sql = *s;//"INSERT INTO test_for_cpp(id, name, age) VALUES(1, 'asdf', 25)";
if (sqlite3_exec(pDB, sql, NULL, NULL, &err_msg) != SQLITE_OK)
{
printf("操作失败,错误代码: %s", err_msg);
//删除出错行,重新commit此队列
rc = sqlite3_exec(pDB, "ROLLBACK;", 0, 0, &err_msg);
slist->remove(*s);
db_fun(pDB , slist);
break;
//return 0;
}
}
rc = sqlite3_exec(pDB, "COMMIT;", 0, 0, &err_msg);
QueryPerformanceCounter((LARGE_INTEGER *)&t2);
QueryPerformanceFrequency((LARGE_INTEGER *)&tc);
float diff = (t2 - t1) / tc;
printf("用时: %f", diff);
return result;
}
DWORD WINAPI Thread_WriteData(LPVOID lpParam)
{
//sqlite3 *pDB = (sqlite3*)lpParam;
int i = (int)lpParam;
char* db_name = "";
char tmp_arr[25];
//itoa(i,tmp_arr,10);
sprintf(tmp_arr,"%d.db3",i);
db_name = tmp_arr;
//strcpy(db_name, tmp_arr);
sqlite3 *pDB = NULL;
char* err_msg = NULL;
char* databaseName=db_name;
int rc = sqlite3_open(databaseName, &pDB);
/*char* sql = "CREATE TABLE test_for_cpp(id int, name varchar(20), age int)";
if (sqlite3_exec(pDB, sql, NULL, NULL, &err_msg) != SQLITE_OK)
{
printf("操作失败,错误代码: %s", err_msg);
return 0;
}*/
if (sqlite3_exec(pDB, "PRAGMA journal_mode=WAL;", NULL, NULL, &err_msg) != SQLITE_OK) {
printf("操作失败,错误代码: %s", err_msg);
return 0;
}
if (sqlite3_exec(pDB, "PRAGMA cache_size=5000;", NULL, NULL, &err_msg) != SQLITE_OK) {
printf("操作失败,错误代码: %s", err_msg);
return 0;
}
if (sqlite3_exec(pDB, "PRAGMA temp_store=MEMORY;", NULL, NULL, &err_msg) != SQLITE_OK) {
printf("操作失败,错误代码: %s", err_msg);
return 0;
}
//开启此参数能提高处理速度,但是系统意外断电时数据文件可能会损坏
/*if (sqlite3_exec(pDB, "PRAGMA synchronous = OFF;", NULL, NULL, &err_msg) != SQLITE_OK) {
printf("操作失败,错误代码: %s", err_msg);
return 0;
}*/
List_Char *slist = new List_Char;
for(int i=0;i<10000;i++)
{
char* sql = "INSERT INTO test_for_cpp(id, name, age) VALUES(1, 'asdf', 25)";
slist->push_back(sql);
if(i==8000)
{
char* sql = "INSERT INTO test_for_cpp2(id, name, age) VALUES(1, 'asdf', '25')";
slist->push_back(sql);
}
}
int rst = db_fun(pDB , slist);
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*SQLiteWrapper sqlite;
if (sqlite.Open("SQLiteWrapper.db")) {
std::cout << "SQLiteWrapper.db created or opened" << std::endl;
}
else {
std::cout << "couldn't open SQLiteWrapper.db" << std::endl;
}
if (sqlite.DirectStatement("create table foo (bar, baz)")) {
std::cout << "table foo created" << std::endl;
}
else {
std::cout << "Couldn't create table foo" << std::endl;
}
if (sqlite.Open("SQLiteWrapper.db3")) {
std::cout << "SQLiteWrapper.db3 created or opened" << std::endl;
}
else {
std::cout << "couldn't open SQLiteWrapper.db" << std::endl;
}*/
// __int64 t1, t2, tc;
// QueryPerformanceCounter((LARGE_INTEGER *)&t1);
// for(int i=0;i<100;i++)
// {
// SQLiteStatement* stmt = sqlite.Statement("insert into foo values (?, ?)");
// if (stmt->Bind(0, 3)) {
// std::cout << "value 3 successfully bound at pos 0" << std::endl;
// }
// else {
// std::cout << "value 3 NOT successfully bound at pos 0: " << sqlite.LastError() << std::endl;
// }
// if (stmt->Bind(1, 4)) {
// std::cout << "value 4 successfully bound at pos 1" << std::endl;
// }
// else {
// std::cout << "value 4 NOT successfully bound at pos 1:" << sqlite.LastError() << std::endl;
// }
// // ******************************** Executing 1st time
// if (stmt->Execute()) {
// std::cout << "statement executed" << std::endl;
// }
// else {
// std::cout << "error executing statement: " << sqlite.LastError() << std::endl;
// }
// if (stmt->Bind(0, 5)) {
// std::cout << "value 5 successfully bound at pos 0" << std::endl;
// }
// else {
// std::cout << "value 5 NOT successfully bound at pos 0" << std::endl;
// }
// if (stmt->Bind(1, 6)) {
// std::cout << "value 6 successfully bound at pos 1" << std::endl;
// }
// else {
// std::cout << "value 6 NOT successfully bound at pos 1" << std::endl;
// }
// // ******************************** Executing 2nd time
// if (stmt->Execute()) {
// std::cout << "statement executed" << std::endl;
// }
// else {
// std::cout << "error executing statement: " << sqlite.LastError() << std::endl;
// }
//}
//
//QueryPerformanceCounter((LARGE_INTEGER *)&t2);
//QueryPerformanceFrequency((LARGE_INTEGER *)&tc);
//float diff = (t2 - t1) / tc;
//printf("用时: %f", diff);
//int rst = db_fun();
//1处理多线程中的消息,消息为ftp到3,入队操作,返回消息;消息为comet到4,http操作,返回消息
//2打开文件 开辟5W长度的操作[写]队列 开启定时器 block队列
//3定时器时间满则做入库操作 release队列,若队列满后关闭文件,丢给执行文件上传线程,回到2,
//4Comet上传
//模拟测试
//DWORD ThreadID;
//for(int i=1; i < 3; i++ )
//{
// HANDLE h_thread = CreateThread(
// NULL, // default security attributes
// 0, // default stack size
// (LPTHREAD_START_ROUTINE) Thread_WriteData,
// (LPVOID)i, // no thread function arguments
// 0, // default creation flags
// &ThreadID); // receive thread identifier
// if( h_thread == NULL )
// {
// printf("CreateThread error: %d\n", GetLastError());
// return 1;
// }
//}
/*using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );*/
Sleep(5000);
//rc = sqlite3_close(pDB);
return 0;
}