/********************************************************************filename: sqlite.hcreated: 2012-11-05author: firehoodpurpose: sqlite数据库 *** 作类*********************************************************************/#pragma once#include <windows.h>#include "..\sqlite\sqlite3.h" #pragma comment(lib,"sqlite.lib") typedef BOol (WINAPI *queryCallback) (voID *para,int n_column,char **column_value,char **column_name);typedef enum _sqlITE_DATATYPE{ sqlITE_DATATYPE_INTEGER = sqlITE_INTEGER,sqlITE_DATATYPE_float = sqlITE_float,sqlITE_DATATYPE_TEXT = sqlITE_TEXT,sqlITE_DATATYPE_BLOB = sqlITE_BLOB,sqlITE_DATATYPE_NulL= sqlITE_NulL,}sqlITE_DATATYPE;class sqlite;class sqliteDataReader{public: sqliteDataReader(sqlite3_stmt *pStmt); ~sqliteDataReader();public: // 读取一行数据 BOol Read(); // 关闭Reader,读取结束后调用 voID Close(); // 总的列数 int ColumnCount(voID); // 获取某列的名称 LPCTSTR Getname(int nCol); // 获取某列的数据类型 sqlITE_DATATYPE GetDataType(int nCol); // 获取某列的值(字符串) LPCTSTR GetStringValue(int nCol); // 获取某列的值(整形) int GetIntValue(int nCol); // 获取某列的值(长整形) long GetInt64Value(int nCol); // 获取某列的值(浮点形) double GetfloatValue(int nCol); // 获取某列的值(二进制数据) const BYTE* GetBlobValue(int nCol,int &nLen);private: sqlite3_stmt *m_pStmt;};class sqliteCommand{public: sqliteCommand(sqlite* psqlite); sqliteCommand(sqlite* psqlite,LPCTSTR lpsql); ~sqliteCommand();public: // 设置命令 BOol SetCommandText(LPCTSTR lpsql); // 绑定参数(index为要绑定参数的序号,从1开始) BOol BindParam(int index,LPCTSTR szValue); BOol BindParam(int index,const int nValue); BOol BindParam(int index,const double dValue); BOol BindParam(int index,const unsigned char* blobValue,int nLen); // 执行命令 BOol Excute(); // 清除命令(命令不再使用时需调用该接口清除) voID Clear();private: sqlite *m_psqlite; sqlite3_stmt *m_pStmt;};class sqlite{public: sqlite(voID); ~sqlite(voID);public: // 打开数据库 BOol Open(LPCTSTR lpDbFlIE); // 关闭数据库 voID Close(); // 执行非查询 *** 作(更新或删除) BOol ExcuteNonquery(LPCTSTR lpsql); BOol ExcuteNonquery(sqliteCommand* pCmd); // 查询 sqliteDataReader Excutequery(LPCTSTR lpsql); // 查询(回调方式) BOol Excutequery(LPCTSTR lpsql,queryCallback pCallBack); // 开始事务 BOol BeginTransaction(); // 提交事务 BOol CommitTransaction(); // 回滚事务 BOol RollbackTransaction(); // 获取上一条错误信息 LPCTSTR GetLastErrorMsg();public: frIEnd class sqliteCommand;private: sqlite3 *m_db;};
/********************************************************************filename: sqlite.cppcreated: 2012-11-05author: firehoodpurpose: sqlite数据库 *** 作类*********************************************************************/#include "sqlite.h"const char* WcharToUtf8(const wchar_t *pwStr){ if (pwStr == NulL) { return NulL; } int len = WIDeCharToMultiByte(CP_UTF8,NulL); if (len <= 0) { return NulL; } char *pStr = new char[len]; WIDeCharToMultiByte(CP_UTF8,NulL); return pStr;}const wchar_t* Utf8ToWchar(const char *pStr){ if (pStr == NulL) { return NulL; } int len = MultiBytetoWIDeChar(CP_UTF8,0); if (len <= 0) { return NulL; } wchar_t *pwStr = new wchar_t[len]; MultiBytetoWIDeChar(CP_UTF8,len); return pwStr;}sqlite::sqlite(voID):m_db(NulL){}sqlite::~sqlite(voID){ Close();}BOol sqlite::Open(LPCTSTR lpDbFlIE){ if(lpDbFlIE == NulL) { return FALSE; }#ifdef UNICODE if(sqlite3_open16(lpDbFlIE,&m_db) != sqlITE_OK)#else if(sqlite3_open(lpDbFlIE,&m_db) != sqlITE_OK)#endif { return FALSE; } return TRUE;}voID sqlite::Close(){ if(m_db) { sqlite3_close(m_db); m_db = NulL; }}BOol sqlite::ExcuteNonquery(LPCTSTR lpsql){ if(lpsql == NulL) { return FALSE; } sqlite3_stmt* stmt; #ifdef UNICODE if(sqlite3_prepare16_v2(m_db,NulL) != sqlITE_OK)#else if(sqlite3_prepare_v2(m_db,NulL) != sqlITE_OK)#endif { return FALSE; } sqlite3_step(stmt); return (sqlite3_finalize(stmt) == sqlITE_OK) ? TRUE : FALSE ;}BOol sqlite::ExcuteNonquery(sqliteCommand* pCmd){ if(pCmd == NulL) { return FALSE; } return pCmd->Excute();}// 查询(回调方式)BOol sqlite::Excutequery(LPCTSTR lpsql,queryCallback pCallBack){ if(lpsql == NulL || pCallBack == NulL) { return FALSE; } char *errmsg = NulL;#ifdef UNICODE const char *szsql = WcharToUtf8(lpsql); if(sqlite3_exec(m_db,&errmsg) != sqlITE_OK) { delete[] szsql; return FALSE; } delete[] szsql;#else if(sqlite3_exec(m_db,&errmsg) != sqlITE_OK) { return FALSE; } #endif return TRUE;}// 查询sqliteDataReader sqlite::Excutequery(LPCTSTR lpsql){ if(lpsql == NulL) { return FALSE; } sqlite3_stmt* stmt; #ifdef UNICODE if(sqlite3_prepare16_v2(m_db,NulL) != sqlITE_OK)#else if(sqlite3_prepare_v2(m_db,NulL) != sqlITE_OK)#endif { return FALSE; } return sqliteDataReader(stmt);}// 开始事务BOol sqlite::BeginTransaction(){ char * errmsg = NulL; if(sqlite3_exec(m_db,"BEGIN TRANSACTION;",&errmsg) != sqlITE_OK) { return FALSE; } return TRUE;}// 提交事务BOol sqlite::CommitTransaction(){ char * errmsg = NulL; if(sqlite3_exec(m_db,"COMMIT TRANSACTION;;",&errmsg) != sqlITE_OK) { return FALSE; } return TRUE;}// 回滚事务BOol sqlite::RollbackTransaction(){ char * errmsg = NulL; if(sqlite3_exec(m_db,"RolLBACK TRANSACTION;",&errmsg) != sqlITE_OK) { return FALSE; } return TRUE;}// 获取上一条错误信息LPCTSTR sqlite::GetLastErrorMsg(){#ifdef UNICODE return (LPCTSTR)sqlite3_errmsg16(m_db);#else return sqlite3_errmsg(m_db);#endif}sqliteDataReader::sqliteDataReader(sqlite3_stmt *pStmt):m_pStmt(pStmt){}sqliteDataReader::~sqliteDataReader(){ Close();}// 读取一行数据BOol sqliteDataReader::Read(){ if(m_pStmt == NulL) { return FALSE; } if(sqlite3_step(m_pStmt) != sqlITE_ROW) { return FALSE; } return TRUE;}// 关闭Reader,读取结束后调用voID sqliteDataReader::Close(){ if(m_pStmt) { sqlite3_finalize(m_pStmt); m_pStmt = NulL; }}// 总的列数int sqliteDataReader::ColumnCount(voID){ return sqlite3_column_count(m_pStmt);}// 获取某列的名称 LPCTSTR sqliteDataReader::Getname(int nCol){#ifdef UNICODE return (LPCTSTR)sqlite3_column_name16(m_pStmt,nCol);#else return (LPCTSTR)sqlite3_column_name(m_pStmt,nCol);#endif}// 获取某列的数据类型sqlITE_DATATYPE sqliteDataReader::GetDataType(int nCol){ return (sqlITE_DATATYPE)sqlite3_column_type(m_pStmt,nCol);}// 获取某列的值(字符串)LPCTSTR sqliteDataReader::GetStringValue(int nCol){#ifdef UNICODE return (LPCTSTR)sqlite3_column_text16(m_pStmt,nCol);#else return (LPCTSTR)sqlite3_column_text(m_pStmt,nCol);#endif}// 获取某列的值(整形)int sqliteDataReader::GetIntValue(int nCol){ return sqlite3_column_int(m_pStmt,nCol);}// 获取某列的值(长整形)long sqliteDataReader::GetInt64Value(int nCol){ return (long)sqlite3_column_int64(m_pStmt,nCol);}// 获取某列的值(浮点形)double sqliteDataReader::GetfloatValue(int nCol){ return sqlite3_column_double(m_pStmt,nCol);}// 获取某列的值(二进制数据)const BYTE* sqliteDataReader::GetBlobValue(int nCol,int &nLen){ nLen = sqlite3_column_bytes(m_pStmt,nCol); return (const BYTE*)sqlite3_column_blob(m_pStmt,nCol);}sqliteCommand::sqliteCommand(sqlite* psqlite):m_psqlite(psqlite),m_pStmt(NulL){}sqliteCommand::sqliteCommand(sqlite* psqlite,LPCTSTR lpsql):m_psqlite(psqlite),m_pStmt(NulL){ SetCommandText(lpsql);}sqliteCommand::~sqliteCommand(){}BOol sqliteCommand::SetCommandText(LPCTSTR lpsql){#ifdef UNICODE if(sqlite3_prepare16_v2(m_psqlite->m_db,NulL) != sqlITE_OK)#else if(sqlite3_prepare_v2(m_psqlite->m_db,NulL) != sqlITE_OK)#endif { return FALSE; } return TRUE;}BOol sqliteCommand::BindParam(int index,LPCTSTR szValue){#ifdef UNICODE if(sqlite3_bind_text16(m_pStmt,sqlITE_TRANSIENT) != sqlITE_OK)#else if(sqlite3_bind_text(m_pStmt,sqlITE_TRANSIENT) != sqlITE_OK)#endif { return FALSE; } return TRUE;}BOol sqliteCommand::BindParam(int index,const int nValue){ if(sqlite3_bind_int(m_pStmt,nValue) != sqlITE_OK) { return FALSE; } return TRUE;}BOol sqliteCommand::BindParam(int index,const double dValue){ if(sqlite3_bind_double(m_pStmt,dValue) != sqlITE_OK) { return FALSE; } return TRUE;}BOol sqliteCommand::BindParam(int index,const unsigned char* blobBuf,int nLen){ if(sqlite3_bind_blob(m_pStmt,NulL) != sqlITE_OK) { return FALSE; } return TRUE;}BOol sqliteCommand::Excute(){ sqlite3_step(m_pStmt); return (sqlite3_reset(m_pStmt) == sqlITE_OK) ? TRUE : FALSE ;}voID sqliteCommand::Clear(){ if(m_pStmt) { sqlite3_finalize(m_pStmt); }}
voID sqliteOperate(){ TCHAR *szDbPath = _T("Book.db"); ::Deletefile(szDbPath); sqlite sqlite; // 打开或创建数据库 //****************************************************** if(!sqlite.Open(szDbPath)) { _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg()); return; } //****************************************************** // 创建数据库表 //****************************************************** TCHAR sql[512] = {0}; _stprintf(sql,_T("%s"),_T("CREATE table [Book] (") _T("[ID] INTEGER NOT NulL PRIMARY KEY,") _T("[name] NVARCHAR(20),") _T("[author] NVARCHAR(20),") _T("[catagory_ID] INTEGER REFERENCES [category]([ID]),") _T("[abstruct] NVARCHAR(100),") _T("[path] NVARCHAR(50),") _T("[image] BLOB);") _T("CREATE INDEX [Book_ID] ON [Book] ([ID]);") ); if(!sqlite.ExcuteNonquery(sql)) { printf("Create database table Failed...\n"); } //****************************************************** // 插入数据【普通方式】 DWORD DWBeginTick = GetTickCount(); //****************************************************** // 当一次性插入多条记录时候,采用事务的方式,提高效率 sqlite.BeginTransaction(); // 批量插入数据 for(int i=0;i<1000;i++) { memset(sql,sizeof(sql)); _stprintf(sql,_T("insert into Book(name,catagory_ID) values('红高粱%d',1)"),i); if(!sqlite.ExcuteNonquery(sql)) { _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg()); break; } } // 提交事务 sqlite.CommitTransaction(); printf("Insert Data Take %dMS...\n",GetTickCount()-DWBeginTick); //****************************************************** // 插入数据【通过参数绑定的方式,提交批量数据时,比上面的普通模式效率更高(提高约45%),同时可支持插入二进制数据】 DWBeginTick = GetTickCount(); //****************************************************** // 当一次性插入多条记录时候,采用事务的方式,提高效率 sqlite.BeginTransaction(); memset(sql,sizeof(sql)); _stprintf(sql,image) values(?,?)")); sqliteCommand cmd(&sqlite,sql); // 批量插入数据 for(int i=0;i<1000;i++) { TCHAR strValue[16] = {0}; _stprintf(strValue,_T("他的国%d"),i); // 绑定第一个参数(name字段值) cmd.BindParam(1,strValue); // 绑定第二个参数(catagory_ID字段值) cmd.BindParam(2,20); BYTE imageBuf[] = {0xff,0xff}; // 绑定第三个参数(image字段值,二进制数据) cmd.BindParam(3,sizeof(imageBuf)); if(!sqlite.ExcuteNonquery(&cmd)) { _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg()); break; } } // 清空cmd cmd.Clear(); // 提交事务 sqlite.CommitTransaction(); printf("Insert Data Take %dMS...\n",GetTickCount()-DWBeginTick); //****************************************************** // 查询 DWBeginTick = GetTickCount(); //****************************************************** memset(sql,_T("select * from Book where name = '他的国345'")); sqliteDataReader Reader = sqlite.Excutequery(sql); int index = 0; int len = 0; while(Reader.Read()) { _tprintf( _T("***************【第%d条记录】***************\n"),++index); _tprintf( _T("字段名:%s 字段值:%d\n"),Reader.GetIntValue(0)); _tprintf( _T("字段名:%s 字段值:%s\n"),Reader.GetStringValue(1)); _tprintf( _T("字段名:%s 字段值:%s\n"),Reader.GetStringValue(2)); _tprintf( _T("字段名:%s 字段值:%d\n"),Reader.GetIntValue(3)); _tprintf( _T("字段名:%s 字段值:%s\n"),Reader.GetStringValue(4)); // 读取图片二进制文件 const BYTE *ImageBuf = Reader.GetBlobValue(6,len); _tprintf( _T("*******************************************\n")); } Reader.Close(); printf("query Take %dMS...\n",GetTickCount()-DWBeginTick); //****************************************************** // 关闭数据库 sqlite.Close();}
运行结果
Insert Data Take 645MS... Insert Data Take 229MS... ***************【第1条记录】*************** 字段名:ID 字段值:1346 字段名:name 字段值:他的国345 字段名:author 字段值:韩寒 字段名:catagory_ID 字段值:20 字段名:abstruct 字段值:(null) ******************************************* query Take 63MS...
总结以上是内存溢出为你收集整理的SQLite C++ *** 作类全部内容,希望文章能够帮你解决SQLite C++ *** 作类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)