sqlite数据库包装类

sqlite数据库包装类,第1张

概述      SQLite 是 D. Richard Hipp 用 C 语言编写的开源嵌入式数据库引擎。它是完全独立的,不具有外部依赖性。 我下面提供一个Sqlite数据库包装类,供读者在相应的场合使用。       SQLite的官方网站下载地址是 http://www.sqlite.org/download.html ,有兴趣的读者可以到这上面去下载最新的版本。       同时,要关注SQLi

sqlite 是 D. Richard Hipp 用 C 语言编写的开源嵌入式数据库引擎。它是完全独立的,不具有外部依赖性。 我下面提供一个sqlite数据库包装类,供读者在相应的场合使用。

sqlite的官方网站下载地址是 http://www.sqlite.org/download.html,有兴趣的读者可以到这上面去下载最新的版本。

同时,要关注sqlite处理语句与标准sql稍有不同的几个地方(比如top语句等),具体的可以去网上搜索。

我的EMAIL是 wxy3064one@163.com

(一) 头文件 h

#ifndef _Q_MEMORY_DB_H
#define _Q_MEMORY_DB_H

#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#pragma comment(lib,"lib/sqlite3.lib")
#include "include/sqlite3.h"
#else
#include <sqlite3.h>
#define sqlITE_THREADSAFE 1//支持多线程
#endif

#ifdef __cplusplus
}
#endif

#define BS_liNE_table_name "T_BL"
#define BS_liNE_DROP_sql "drop table T_BL;"
#define BS_liNE_CREAT_sql "create table T_BL(sqlITE_ID integer primary key,/
LON real,/
LAT real,/
CELLID integer,/
LAC integer); "
#define BS_liNE_INSERT_sql "insert into T_BL values(?,?,?); "
#define BS_liNE_CREATE_INDEX_CI_LAC_sql "create index IDx_cellID_lac on T_BL(CELLID,LAC);"

class cmemDB
{
private:
bool InMemory;
int nTimeOut;
bool eof;
sqlite3 *db;
sqlite3_stmt* stmt;
int sql_code;
int total_rows,total_cols;
voID Finalize();
voID Close();
bool Existtable(CString tbname);
public:

enum MemDBFIEldType{ft_INT = 1,ft_float,ft_TEXT,ft_BLOB,ft_NulL};

cmemDB(int ATimeOut=60000);
virtual ~cmemDB();

voID CreateMemoryDB();
const char* GetErrMsg();
//使用sqlite3_stmt
//执行带绑定参数的SQL语句
voID Preparesql(CString szsql);
bool Bind(int nCol,const char* szText);
bool Bind(int nCol,const int nValue);
bool Bind(int nCol,const double DWValue);
bool Bind(int nCol,const BYTE* vBlob,int nLen);
bool BindNulL(int nCol);
int Exec();
//执行SQL语句(有返回结果)
int Execsql(CString szsql);
//直接执行(无返回结果)
int ExecCallBacksql(CString szsql,sqlite3_callback func = 0,voID* para= NulL);//带回调函数的sql
//直接执行END
//字段 *** 作
int Findindex(CString szFIEld);
const char* FIEldname(int nCol);
const int GetFIEldType(CString szFIEld);
const int GetFIEldType(int nCol);
bool FIEldisNulL(CString szFIEld);
bool FIEldisNulL(int nCol);
int GetIntFIEldByname(CString szFIEld,int nDefault=0);
int GetIntFIEldByIndex(int nCol,int nDefault=0);
double GetDoubleFIEldByname(CString szFIEld,double fDefault=0.00);
double GetDoubleFIEldByIndex(int nCol,double fDefault=0.00);
const char* GetTextFIEldByname(CString szFIEld,const char* sDefault="");
const char* GetTextFIEldByIndex(int nCol,const char* sDefault="");
const BYTE* GetBlobFIEldByname(CString szFIEld,int *nLen);
const BYTE* GetBlobFIEldByIndex(int nCol,int *nLen);
//行列 *** 作
bool Eof();
voID reset();
int Next();
int GetCols();
//使用sqlite3_stmt END
//Other Operation
bool BeginTran();
bool CommitTran();
bool RollbackTran();
voID setTimeOut(int ATimeOut);
voID Interrupt();
sqlite_int64 GetLastRowID();
//特殊应用
protected:
CString Curtablename;

public:
//针对基站和DT的拉线
bool FindBLKey(CString ACIValue,CString ALACValue);
voID CreateBLtable();

//读取shape文件的dbf(主要是点数据的文本内容,线和面一般不做处理)
bool FindKey(CString szID);
CString GetValue(CString oID,int nFIEldindex);
voID AddDBFromfile(CString dbfilename);

};
#endif

(二) 实现文件

#include "stdafx.h"
#include "kml_baseclass.h"
#include "MapfileReader.h"
#include "MemDB.h"

cmemDB::cmemDB(int ATimeOut)
{
Curtablename = "";
db = NulL; stmt = NulL;eof = false;
InMemory = false;
total_cols = -1;total_rows = -1;;
sql_code = sqlite3_open(":memory:",&db);
if (sql_code == sqlITE_OK)
InMemory = true;
setTimeOut(ATimeOut);
}
cmemDB::~cmemDB()
{
Close();
}

voID cmemDB::CreateMemoryDB()
{
Close();
sql_code = sqlite3_open(":memory:",&db);
if (sql_code == sqlITE_OK)
InMemory = true;
}
voID cmemDB::Finalize()
{
if (stmt)
{
sql_code = sqlite3_finalize(stmt);
stmt = NulL;
}
}
voID cmemDB::Close()
{
Finalize();
if (db)
{
sqlite3_close(db);
db = NulL;
}
InMemory = false;
}

bool cmemDB::Existtable(CString tbname)
{
bool ret = false;
if (db)
{
if (Execsql("select count(*) from sqlite_master where type='table' and name='"+tbname+"'") != -1)
{
if (GetIntFIEldByIndex(0)==1)
{
ret =true;
}
}
}
return ret;
}

const char* cmemDB::GetErrMsg()
{
return db?sqlite3_errmsg(db):"";
}

int cmemDB::ExecCallBacksql(CString szsql,sqlite3_callback func,voID* para)
{
int ret = -1;
char* szMsg = NulL;
if (db)
{
sql_code = sqlite3_exec(db,AnsiToUtf8(szsql),func,para,&szMsg);
if (sql_code == sqlITE_OK)
ret = 0;
if (szMsg != NulL)
{
sqlite3_free(szMsg);
szMsg = NulL;
}
}
return ret;
}
voID cmemDB::Preparesql(CString szsql)
{
Finalize();
if ((sql_code = sqlite3_prepare(db,-1,&stmt,0)) != sqlITE_OK)
{
stmt = NulL;
}
}
bool cmemDB::Bind(int nCol,const char* szText)
{
bool bRet = false;
if (stmt != NulL)
{
if ((sql_code =sqlite3_bind_text(stmt,nCol+1,szText,sqlITE_TRANSIENT)) == sqlITE_OK)
bRet = true;
}
return bRet;
}
bool cmemDB::Bind(int nCol,const int nValue)
{
bool bRet = false;
if (stmt != NulL)
{
if ((sql_code =sqlite3_bind_int(stmt,nValue)) == sqlITE_OK)
bRet = true;
}
return bRet;
}
bool cmemDB::Bind(int nCol,const double DWValue)
{
bool bRet = false;
if (stmt != NulL)
{
if ((sql_code =sqlite3_bind_double(stmt,DWValue)) == sqlITE_OK)
bRet = true;
}
return bRet;
}
bool cmemDB::Bind(int nCol,int nLen)
{
bool bRet = false;
if (stmt != NulL)
{
if ((sql_code = sqlite3_bind_blob(stmt,(const voID*)vBlob,nLen,sqlITE_TRANSIENT)) == sqlITE_OK)
bRet = true;
}
return bRet;
}
bool cmemDB::BindNulL(int nCol)
{
bool bRet = false;
if (stmt != NulL)
{
if ((sql_code =sqlite3_bind_null(stmt,nCol+1)) == sqlITE_OK)
bRet = true;
}
return bRet;
}
int cmemDB::Exec()
{
int ret = -1;
if (stmt != NulL && db != NulL)
{
if ((sql_code = sqlite3_step(stmt)) == sqlITE_DONE)
{
ret = sqlite3_changes(db);
}
//sql_code = sqlite3_reset(stmt);
}
return ret;
}
int cmemDB::Execsql(CString szsql)
{
int ret = -1;
if (db)
{
Preparesql(szsql);
if (stmt)
{
sql_code = sqlite3_step(stmt);
if (sql_code == sqlITE_DONE)
{
eof = true;
//ret = 0;
}else if (sql_code == sqlITE_ROW)
{
eof = false;
ret = 0;
}else {
Finalize();
total_rows = 0;
total_cols = 0;
}
if (stmt)
{
total_cols = sqlite3_column_count(stmt);
}
}
}
return ret;
}

int cmemDB::Findindex(CString szFIEld)
{
int index = -1;
CString tmpstr,tmpfIEld;
if (stmt != NulL)
{
tmpfIEld = szFIEld;
tmpfIEld.Trimleft();
tmpfIEld.TrimRight();
if (tmpfIEld != "")
{
for(int i = 0;i<total_cols;i++)
{
const char* sztmp = sqlite3_column_name(stmt,i);
tmpstr = sztmp;
tmpstr.MakeUpper();
if ( sztmp != 0 && strcmp((LPCTSTR)tmpfIEld,(LPCTSTR)tmpstr)==0)
{
index = i;
break;
}
}
}
}
return index;
}
const char* cmemDB::FIEldname(int nCol)
{
if (!stmt) return NulL;
if (!(nCol <0 || nCol > total_cols - 1))
return sqlite3_column_name(stmt,nCol);
else return NulL;
}
const int cmemDB::GetFIEldType(CString szFIEld)
{
int index = -1;
if (stmt == NulL)
return index;
index = Findindex(szFIEld);
if (index == -1)
return index;
else
return sqlite3_column_type(stmt,index);
}
const int cmemDB::GetFIEldType(int nCol)
{
int index = -1;
if (stmt == NulL)
return index;
if (nCol <0 || nCol > total_cols - 1)
return index;
else
return sqlite3_column_type(stmt,nCol);
}
bool cmemDB::FIEldisNulL(CString szFIEld)
{
int index;
bool bRet = true;
if ((index = GetFIEldType(szFIEld)) != -1)
bRet = (index == sqlITE_NulL);
return bRet;
}
bool cmemDB::FIEldisNulL(int nCol)
{
int index;
bool bRet = true;
if ((index = GetFIEldType(nCol)) != -1)
bRet = (index == sqlITE_NulL);
return bRet;
}
int cmemDB::GetIntFIEldByname(CString szFIEld,int nDefault)
{
if (!stmt)
return nDefault;
else{
int index = Findindex(szFIEld);
if (index == -1) return nDefault;
if (FIEldisNulL(szFIEld)) return nDefault;
return sqlite3_column_int(stmt,index);
}
}
int cmemDB::GetIntFIEldByIndex(int nCol,int nDefault)
{
if (!stmt)
return nDefault;
else{
if (FIEldisNulL(nCol)) return nDefault;
return sqlite3_column_int(stmt,nCol);
}
}
double cmemDB::GetDoubleFIEldByname(CString szFIEld,double fDefault)
{
if (!stmt)
return fDefault;
else{
int index = Findindex(szFIEld);
if (index == -1) return fDefault;
if (FIEldisNulL(szFIEld)) return fDefault;
return sqlite3_column_double(stmt,index);
}
}
double cmemDB::GetDoubleFIEldByIndex(int nCol,double fDefault)
{
if (!stmt)
return fDefault;
else{
if (FIEldisNulL(nCol)) return fDefault;
return sqlite3_column_double(stmt,nCol);
}
}
const char* cmemDB::GetTextFIEldByname(CString szFIEld,const char* sDefault)
{
if (!stmt)
return sDefault;
else{
int index = Findindex(szFIEld);
if (index == -1) return sDefault;
if (FIEldisNulL(szFIEld)) return sDefault;
return (const char*)sqlite3_column_text(stmt,index);
}
}
const char* cmemDB::GetTextFIEldByIndex(int nCol,const char* sDefault)
{
if (!stmt)
return sDefault;
else{
if (FIEldname(nCol) == NulL) return sDefault;
if (FIEldisNulL(nCol)) return sDefault;
return (const char*)sqlite3_column_text(stmt,nCol);
}
}
const BYTE* cmemDB::GetBlobFIEldByname(CString szFIEld,int *nLen)
{
BYTE* pData = 0;
*nLen = 0;
if (!FIEldisNulL(szFIEld))
{
int index = Findindex(szFIEld);
if (index != -1)
{
*nLen = sqlite3_column_bytes(stmt,index);
pData = (BYTE*)sqlite3_column_blob(stmt,index);
}
}
return pData;
}
const BYTE* cmemDB::GetBlobFIEldByIndex(int nCol,int *nLen)
{
BYTE* pData = 0;
*nLen = 0;
if (!FIEldisNulL(nCol))
{
*nLen = sqlite3_column_bytes(stmt,nCol);
pData = (BYTE*)sqlite3_column_blob(stmt,nCol);
}
return pData;
}
bool cmemDB::Eof()
{
return eof;
}
voID cmemDB::reset()
{
if(stmt)
{
eof = !(sqlite3_reset(stmt)==sqlITE_OK);
}
}
int cmemDB::Next()
{
int ret = -1;
if (stmt)
{
sql_code = sqlite3_step(stmt);
if (sql_code == sqlITE_DONE)
{
eof = true;
ret = 0;
}else if (sql_code == sqlITE_ROW)
{
eof = false;
ret = 0;
}else {
Finalize();
}
}
return ret;
}
int cmemDB::GetCols()
{
return total_cols;
}
bool cmemDB::BeginTran()
{
return (ExecCallBacksql("begin transaction;")!=-1);
}
bool cmemDB::CommitTran()
{
return (ExecCallBacksql("commit transaction;")!=-1);
}
bool cmemDB::RollbackTran()
{
return (ExecCallBacksql("rollback transaction;")!=-1);
}
voID cmemDB::setTimeOut(int ATimeOut)
{
if (db)
{
nTimeOut = ATimeOut;
sqlite3_busy_timeout(db,nTimeOut);
}
}
voID cmemDB::Interrupt()
{
if (db)
{
sqlite3_interrupt(db);
}
}

sqlite_int64 cmemDB::GetLastRowID()
{
return sqlite3_last_insert_rowID(db);
}

voID cmemDB::CreateBLtable()
{
if (Existtable(BS_liNE_table_name))
{
ExecCallBacksql(BS_liNE_DROP_sql);
}
ExecCallBacksql(BS_liNE_CREAT_sql);
ExecCallBacksql(BS_liNE_CREATE_INDEX_CI_LAC_sql);
Curtablename = BS_liNE_table_name;
}

bool cmemDB::FindBLKey(CString ACIValue,CString ALACValue)
{
CString szsql;
bool ret = false;
if (db && Curtablename.IsEmpty() == false)
{
szsql = "select * from " + Curtablename + " where CELLID="+ACIValue+" and LAC="+ALACValue + " limit 0,1";
if (Execsql(szsql) == 0)
ret = true;
}
return ret;
}

bool cmemDB::FindKey(CString szID)
{
CString szsql;
bool ret = false;
if (db && Curtablename.IsEmpty() == false)
{
szsql = "select * from " + Curtablename + " where sqlITE_ID ="+szID;
if (Execsql(szsql) == 0)
ret = true;
}
return ret;
}

CString cmemDB::GetValue(CString oID,int nFIEldindex)
{
CString ret = "";
if (FindKey(oID))
{
switch(GetFIEldType(nFIEldindex))
{
case cmemDB::ft_TEXT: {
ret = GetTextFIEldByIndex(nFIEldindex);
}break;
case cmemDB::ft_INT: ret = IntToStr(GetIntFIEldByIndex(nFIEldindex)); break;
case cmemDB::ft_float:ret = floatToStr(GetDoubleFIEldByIndex(nFIEldindex)); break;
default:ret = "";break;
}
}
return ret;
}

voID cmemDB::AddDBFromfile(CString dbfilename)
{
int i,j;
int NumRecs,FIEldCount;
CString fname,ext;
CString dbSelectsql,dbCreatesql,dbInsertsql;
CDBFfileReader* dbfile;
if (dbfilename.IsEmpty()==false)
{
if (!InMemory)
CreateMemoryDB();
dbfile = new CDBFfileReader();
if (dbfile->Open(dbfilename))
{
fname = Extractfilename(dbfilename);
ext = ExtractfileExt(dbfilename);
fname = fname.MID(0,fname.GetLength() - ext.GetLength());
FIEldCount = dbfile->GetFIEldCount();
dbSelectsql = "select * from "+fname+";";
if (!Existtable(fname))
{
dbCreatesql = "create table "+fname+" (sqlITE_ID integer primary key ";
dbInsertsql = "insert into "+fname;
for(i=0;i<FIEldCount;i++)
{
switch(dbfile->GetFIEldTypeByIndex(i))
{
case 'C':{
dbCreatesql += ","+dbfile->GetFIEldnameByIndex(i)+" char("+IntToStr(dbfile->GetFIEldLenByIndex(i))+")";
}break;
case 'F':{
dbCreatesql += ","+dbfile->GetFIEldnameByIndex(i)+" real ";
}break;
case 'N':{
dbCreatesql += ","+dbfile->GetFIEldnameByIndex(i)+" integer ";
}break;
case 'D':{
dbCreatesql += ","+dbfile->GetFIEldnameByIndex(i)+" char(8)";//date
}break;
case 'L':{
dbCreatesql += ","+dbfile->GetFIEldnameByIndex(i)+" char(1)";
}break;
case 'M':
default:break;
}
}
dbCreatesql += ");";
dbInsertsql+=" values(?";
for(i=0;i<FIEldCount;i++)
dbInsertsql+=",?";
dbInsertsql += ");";
ExecCallBacksql(dbCreatesql);
}
Curtablename = fname;

j = 0;NumRecs = dbfile->GetRecordCount();BeginTran();while(j<NumRecs){Preparesql(dbInsertsql);Bind(0,j+1);for(i=0;i<FIEldCount;i++){CString str = Trim(dbfile->ReadAttr(j+1,i));switch(dbfile->GetFIEldTypeByIndex(i)){case 'C':case 'D':case 'L':{Bind(i+1,(LPCTSTR)str);}break;case 'F':{Bind(i+1,StrTofloat(str));}break;case 'N':{Bind(i+1,StrToInt(str));}break;case 'M':default:break;} }Exec();j++;if (j%1000==0){CommitTran();BeginTran();}}CommitTran();Execsql(dbSelectsql);}delete dbfile;}}

总结

以上是内存溢出为你收集整理的sqlite数据库包装类全部内容,希望文章能够帮你解决sqlite数据库包装类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1177800.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存