头文件(sqlite.h)
- /********************************************************************
- filename:sqlite.h
- created:2012-11-05
- author:firehood
- purpose:sqlite数据库操作类
- *********************************************************************/
- #pragmaonce
- #include<windows.h>
- #include"..\sqlite\sqlite3.h"
- #pragmacomment(lib,"sqlite.lib")
- typedefBOOL(WINAPI*QueryCallback)(void*para,intn_column,char**column_value,char**column_name);
- typedefenum_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;
- classsqlite;
- classsqliteDataReader
- {
- public:
- sqliteDataReader(sqlite3_stmt*pStmt);
- ~sqliteDataReader();
- public:
- //读取一行数据
- BOOLRead();
- //关闭Reader,读取结束后调用
- voidClose();
- //总的列数
- intColumnCount(void);
- //获取某列的名称
- LPCTSTRGetName(intnCol);
- //获取某列的数据类型
- sqlITE_DATATYPEGetDataType(intnCol);
- //获取某列的值(字符串)
- LPCTSTRGetStringValue(intnCol);
- //获取某列的值(整形)
- intGetIntValue(intnCol);
- //获取某列的值(长整形)
- longGetInt64Value(intnCol);
- //获取某列的值(浮点形)
- doubleGetFloatValue(intnCol);
- //获取某列的值(二进制数据)
- constBYTE*GetBlobValue(intnCol,int&nLen);
- private:
- sqlite3_stmt*m_pStmt;
- };
- classsqliteCommand
- {
- public:
- sqliteCommand(sqlite*psqlite);
- sqliteCommand(sqlite*psqlite,LPCTSTRlpsql);
- ~sqliteCommand();
- public:
- //设置命令
- BOOLSetCommandText(LPCTSTRlpsql);
- //绑定参数(index为要绑定参数的序号,从1开始)
- BOOLBindParam(intindex,LPCTSTRszValue);
- BOOLBindParam(intindex,constintnValue);
- BOOLBindParam(intindex,constdoubledValue);
- BOOLBindParam(intindex,constunsignedchar*blobValue,intnLen);
- //执行命令
- BOOLExcute();
- //清除命令(命令不再使用时需调用该接口清除)
- voidClear();
- private:
- sqlite*m_psqlite;
- sqlite3_stmt*m_pStmt;
- };
- classsqlite
- {
- public:
- sqlite(void);
- ~sqlite(void);
- public:
- //打开数据库
- BOOLOpen(LPCTSTRlpDbFlie);
- //关闭数据库
- voidClose();
- //执行非查询操作(更新或删除)
- BOOLExcuteNonQuery(LPCTSTRlpsql);
- BOOLExcuteNonQuery(sqliteCommand*pCmd);
- //查询
- sqliteDataReaderExcuteQuery(LPCTSTRlpsql);
- //查询(回调方式)
- BOOLExcuteQuery(LPCTSTRlpsql,QueryCallbackpCallBack);
- //开始事务
- BOOLBeginTransaction();
- //提交事务
- BOOLCommitTransaction();
- //回滚事务
- BOOLRollbackTransaction();
- //获取上一条错误信息
- LPCTSTRGetLastErrorMsg();
- public:
- friendclasssqliteCommand;
- private:
- sqlite3*m_db;
- };
/******************************************************************** filename: sqlite.h created: 2012-11-05 author: firehood purpose: 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; };
源文件(sqlite.cpp)
- /********************************************************************
- filename:sqlite.cpp
- created:2012-11-05
- author:firehood
- purpose:sqlite数据库操作类
- *********************************************************************/
- #include"sqlite.h"
- constchar*WcharToUtf8(constwchar_t*pwStr)
- {
- if(pwStr==NULL)
- {
- returnNULL;
- }
- intlen=WideCharToMultiByte(CP_UTF8,pwStr,-1,NULL,NULL);
- if(len<=0)
- {
- returnNULL;
- }
- char*pStr=newchar[len];
- WideCharToMultiByte(CP_UTF8,pStr,len,NULL);
- returnpStr;
- }
- constwchar_t*Utf8ToWchar(constchar*pStr)
- {
- if(pStr==NULL)
- {
- returnNULL;
- }
- intlen=MultiByteToWideChar(CP_UTF8,0);
- if(len<=0)
- {
- returnNULL;
- }
- wchar_t*pwStr=newwchar_t[len];
- MultiByteToWideChar(CP_UTF8,len);
- returnpwStr;
- }
- sqlite::sqlite(void):
- m_db(NULL)
- {
- }
- sqlite::~sqlite(void)
- {
- Close();
- }
- BOOLsqlite::Open(LPCTSTRlpDbFlie)
- {
- if(lpDbFlie==NULL)
- {
- returnFALSE;
- }
- #ifdefUNICODE
- if(sqlite3_open16(lpDbFlie,&m_db)!=sqlITE_OK)
- #else
- if(sqlite3_open(lpDbFlie,&m_db)!=sqlITE_OK)
- #endif
- {
- returnFALSE;
- }
- returnTRUE;
- }
- voidsqlite::Close()
- {
- if(m_db)
- {
- sqlite3_close(m_db);
- m_db=NULL;
- }
- }
- BOOLsqlite::ExcuteNonQuery(LPCTSTRlpsql)
- {
- if(lpsql==NULL)
- {
- returnFALSE;
- }
- sqlite3_stmt*stmt;
- #ifdefUNICODE
- if(sqlite3_prepare16_v2(m_db,lpsql,&stmt,NULL)!=sqlITE_OK)
- #else
- if(sqlite3_prepare_v2(m_db,NULL)!=sqlITE_OK)
- #endif
- {
- returnFALSE;
- }
- sqlite3_step(stmt);
- return(sqlite3_finalize(stmt)==sqlITE_OK)?TRUE:FALSE;
- }
- BOOLsqlite::ExcuteNonQuery(sqliteCommand*pCmd)
- {
- if(pCmd==NULL)
- {
- returnFALSE;
- }
- returnpCmd->Excute();
- }
- //查询(回调方式)
- BOOLsqlite::ExcuteQuery(LPCTSTRlpsql,QueryCallbackpCallBack)
- {
- if(lpsql==NULL||pCallBack==NULL)
- {
- returnFALSE;
- }
- char*errmsg=NULL;
- #ifdefUNICODE
- constchar*szsql=WcharToUtf8(lpsql);
- if(sqlite3_exec(m_db,szsql,pCallBack,&errmsg)!=sqlITE_OK)
- {
- delete[]szsql;
- returnFALSE;
- }
- delete[]szsql;
- #else
- if(sqlite3_exec(m_db,&errmsg)!=sqlITE_OK)
- {
- returnFALSE;
- }
- #endif
- returnTRUE;
- }
- //查询
- sqliteDataReadersqlite::ExcuteQuery(LPCTSTRlpsql)
- {
- if(lpsql==NULL)
- {
- returnFALSE;
- }
- sqlite3_stmt*stmt;
- #ifdefUNICODE
- if(sqlite3_prepare16_v2(m_db,NULL)!=sqlITE_OK)
- #else
- if(sqlite3_prepare_v2(m_db,NULL)!=sqlITE_OK)
- #endif
- {
- returnFALSE;
- }
- returnsqliteDataReader(stmt);
- }
- //开始事务
- BOOLsqlite::BeginTransaction()
- {
- char*errmsg=NULL;
- if(sqlite3_exec(m_db,"BEGINTRANSACTION;",&errmsg)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- //提交事务
- BOOLsqlite::CommitTransaction()
- {
- char*errmsg=NULL;
- if(sqlite3_exec(m_db,"COMMITTRANSACTION;;",&errmsg)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- //回滚事务
- BOOLsqlite::RollbackTransaction()
- {
- char*errmsg=NULL;
- if(sqlite3_exec(m_db,"ROLLBACKTRANSACTION;",&errmsg)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- //获取上一条错误信息
- LPCTSTRsqlite::GetLastErrorMsg()
- {
- #ifdefUNICODE
- return(LPCTSTR)sqlite3_errmsg16(m_db);
- #else
- returnsqlite3_errmsg(m_db);
- #endif
- }
- sqliteDataReader::sqliteDataReader(sqlite3_stmt*pStmt):
- m_pStmt(pStmt)
- {
- }
- sqliteDataReader::~sqliteDataReader()
- {
- Close();
- }
- //读取一行数据
- BOOLsqliteDataReader::Read()
- {
- if(m_pStmt==NULL)
- {
- returnFALSE;
- }
- if(sqlite3_step(m_pStmt)!=sqlITE_ROW)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- //关闭Reader,读取结束后调用
- voidsqliteDataReader::Close()
- {
- if(m_pStmt)
- {
- sqlite3_finalize(m_pStmt);
- m_pStmt=NULL;
- }
- }
- //总的列数
- intsqliteDataReader::ColumnCount(void)
- {
- returnsqlite3_column_count(m_pStmt);
- }
- //获取某列的名称
- LPCTSTRsqliteDataReader::GetName(intnCol)
- {
- #ifdefUNICODE
- return(LPCTSTR)sqlite3_column_name16(m_pStmt,nCol);
- #else
- return(LPCTSTR)sqlite3_column_name(m_pStmt,nCol);
- #endif
- }
- //获取某列的数据类型
- sqlITE_DATATYPEsqliteDataReader::GetDataType(intnCol)
- {
- return(sqlITE_DATATYPE)sqlite3_column_type(m_pStmt,nCol);
- }
- //获取某列的值(字符串)
- LPCTSTRsqliteDataReader::GetStringValue(intnCol)
- {
- #ifdefUNICODE
- return(LPCTSTR)sqlite3_column_text16(m_pStmt,nCol);
- #else
- return(LPCTSTR)sqlite3_column_text(m_pStmt,nCol);
- #endif
- }
- //获取某列的值(整形)
- intsqliteDataReader::GetIntValue(intnCol)
- {
- returnsqlite3_column_int(m_pStmt,nCol);
- }
- //获取某列的值(长整形)
- longsqliteDataReader::GetInt64Value(intnCol)
- {
- return(long)sqlite3_column_int64(m_pStmt,nCol);
- }
- //获取某列的值(浮点形)
- doublesqliteDataReader::GetFloatValue(intnCol)
- {
- returnsqlite3_column_double(m_pStmt,nCol);
- }
- //获取某列的值(二进制数据)
- constBYTE*sqliteDataReader::GetBlobValue(intnCol,int&nLen)
- {
- nLen=sqlite3_column_bytes(m_pStmt,nCol);
- return(constBYTE*)sqlite3_column_blob(m_pStmt,nCol);
- }
- sqliteCommand::sqliteCommand(sqlite*psqlite):
- m_psqlite(psqlite),
- m_pStmt(NULL)
- {
- }
- sqliteCommand::sqliteCommand(sqlite*psqlite,LPCTSTRlpsql):
- m_psqlite(psqlite),
- m_pStmt(NULL)
- {
- SetCommandText(lpsql);
- }
- sqliteCommand::~sqliteCommand()
- {
- }
- BOOLsqliteCommand::SetCommandText(LPCTSTRlpsql)
- {
- #ifdefUNICODE
- if(sqlite3_prepare16_v2(m_psqlite->m_db,&m_pStmt,NULL)!=sqlITE_OK)
- #else
- if(sqlite3_prepare_v2(m_psqlite->m_db,NULL)!=sqlITE_OK)
- #endif
- {
- returnFALSE;
- }
- returnTRUE;
- }
- BOOLsqliteCommand::BindParam(intindex,LPCTSTRszValue)
- {
- #ifdefUNICODE
- if(sqlite3_bind_text16(m_pStmt,index,szValue,sqlITE_TRANSIENT)!=sqlITE_OK)
- #else
- if(sqlite3_bind_text(m_pStmt,sqlITE_TRANSIENT)!=sqlITE_OK)
- #endif
- {
- returnFALSE;
- }
- returnTRUE;
- }
- BOOLsqliteCommand::BindParam(intindex,constintnValue)
- {
- if(sqlite3_bind_int(m_pStmt,nValue)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- BOOLsqliteCommand::BindParam(intindex,constdoubledValue)
- {
- if(sqlite3_bind_double(m_pStmt,dValue)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- BOOLsqliteCommand::BindParam(intindex,constunsignedchar*blobBuf,intnLen)
- {
- if(sqlite3_bind_blob(m_pStmt,blobBuf,nLen,NULL)!=sqlITE_OK)
- {
- returnFALSE;
- }
- returnTRUE;
- }
- BOOLsqliteCommand::Excute()
- {
- sqlite3_step(m_pStmt);
- return(sqlite3_reset(m_pStmt)==sqlITE_OK)?TRUE:FALSE;
- }
- voidsqliteCommand::Clear()
- {
- if(m_pStmt)
- {
- sqlite3_finalize(m_pStmt);
- }
- }
/******************************************************************** filename: sqlite.cpp created: 2012-11-05 author: firehood purpose: 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); } }
使用方法
- voidsqliteOperate()
- {
- TCHAR*szDbPath=_T("Book.db");
- ::DeleteFile(szDbPath);
- sqlitesqlite;
- //打开或创建数据库
- //******************************************************
- if(!sqlite.Open(szDbPath))
- {
- _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg());
- return;
- }
- //******************************************************
- //创建数据库表
- //******************************************************
- TCHARsql[512]={0};
- _stprintf(sql,_T("%s"),
- _T("CREATETABLE[Book](")
- _T("[id]INTEGERNOTNULLPRIMARYKEY,")
- _T("[name]NVARCHAR(20),")
- _T("[author]NVARCHAR(20),")
- _T("[catagory_id]INTEGERREFERENCES[Category]([id]),")
- _T("[abstruct]NVARCHAR(100),")
- _T("[path]NVARCHAR(50),")
- _T("[image]BLOB);")
- _T("CREATEINDEX[Book_id]ON[Book]([id]);")
- );
- if(!sqlite.ExcuteNonQuery(sql))
- {
- printf("CreatedatabasetableFailed...\n");
- }
- //******************************************************
- //插入数据【普通方式】
- DWORDdwBeginTick=GetTickCount();
- //******************************************************
- //当一次性插入多条记录时候,采用事务的方式,提高效率
- sqlite.BeginTransaction();
- //批量插入数据
- for(inti=0;i<1000;i++)
- {
- memset(sql,sizeof(sql));
- _stprintf(sql,_T("insertintoBook(name,author,catagory_id)values('红高粱%d','莫言',1)"),i);
- if(!sqlite.ExcuteNonQuery(sql))
- {
- _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg());
- break;
- }
- }
- //提交事务
- sqlite.CommitTransaction();
- printf("InsertDataTake%dMS...\n",GetTickCount()-dwBeginTick);
- //******************************************************
- //插入数据【通过参数绑定的方式,提交批量数据时,比上面的普通模式效率更高(提高约45%),同时可支持插入二进制数据】
- dwBeginTick=GetTickCount();
- //******************************************************
- //当一次性插入多条记录时候,采用事务的方式,提高效率
- sqlite.BeginTransaction();
- memset(sql,sizeof(sql));
- _stprintf(sql,catagory_id,image)values(?,'韩寒',?,?)"));
- sqliteCommandcmd(&sqlite,sql);
- //批量插入数据
- for(inti=0;i<1000;i++)
- {
- TCHARstrValue[16]={0};
- _stprintf(strValue,_T("他的国%d"),i);
- //绑定第一个参数(name字段值)
- cmd.BindParam(1,strValue);
- //绑定第二个参数(catagory_id字段值)
- cmd.BindParam(2,20);
- BYTEimageBuf[]={0xff,0xff,0xff};
- //绑定第三个参数(image字段值,二进制数据)
- cmd.BindParam(3,imageBuf,sizeof(imageBuf));
- if(!sqlite.ExcuteNonQuery(&cmd))
- {
- _tprintf(_T("%s\n"),sqlite.GetLastErrorMsg());
- break;
- }
- }
- //清空cmd
- cmd.Clear();
- //提交事务
- sqlite.CommitTransaction();
- printf("InsertDataTake%dMS...\n",GetTickCount()-dwBeginTick);
- //******************************************************
- //查询
- dwBeginTick=GetTickCount();
- //******************************************************
- memset(sql,_T("select*fromBookwherename='他的国345'"));
- sqliteDataReaderReader=sqlite.ExcuteQuery(sql);
- intindex=0;
- intlen=0;
- while(Reader.Read())
- {
- _tprintf(_T("***************【第%d条记录】***************\n"),++index);
- _tprintf(_T("字段名:%s字段值:%d\n"),Reader.GetName(0),Reader.GetIntValue(0));
- _tprintf(_T("字段名:%s字段值:%s\n"),Reader.GetName(1),Reader.GetStringValue(1));
- _tprintf(_T("字段名:%s字段值:%s\n"),Reader.GetName(2),Reader.GetStringValue(2));
- _tprintf(_T("字段名:%s字段值:%d\n"),Reader.GetName(3),Reader.GetIntValue(3));
- _tprintf(_T("字段名:%s字段值:%s\n"),Reader.GetName(4),Reader.GetStringValue(4));
- //读取图片二进制文件
- constBYTE*ImageBuf=Reader.GetBlobValue(6,len);
- _tprintf(_T("*******************************************\n"));
- }
- Reader.Close();
- printf("QueryTake%dMS...\n",GetTickCount()-dwBeginTick);
- //******************************************************
- //关闭数据库
- sqlite.Close();
- }
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...
原文链接:https://www.f2er.com/sqlite/200912.html