sqlite3数据库Native C++封装类(Unicode)Cppsqlite3U的初步认识与使用
(1)
http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers
从上面的网址可以找到对sqlite数据库的C API的各种语言的封装。包括c、c++、vb、c#.net、delphi、Lisp、D、Java、Javascript、Objective-C、Perl、PHP、Python、Ruby、Lua、Fortran等等。从这也可以看出sqlite数据库作为本地数据存储工具而得到的广泛应用。单单是“C++ Wrappers”也有几十种,常见的如:easysqlite、sqlite++、Cppsqlite、CppsqliteU等等。因为我通常是在Windows CE嵌入式操作系统上开发APP,而Windows CE操作系统只支持Unicode字符集,所以今天要学习和测试的是Unicode版“C++ Wrappers”:CppsqliteU。
(2)
因为是在Windows CE操作系统上使用sqlite,所以在使用CppsqliteU之前,要先编译针对Windows CE平台的sqlite3 DLL。这一步省了,我直接借用了别人已经编译好的。
(3)
下面是一个简单测试示例,主要是利用class Cppsqlite3DB新建数据库、创建表、插入删除更新记录、使用事务、查询记录等等。
- #definesqlITE3_FILE_NAMETEXT("sqlite.db3")
- //获取程序当前路径
- voidGetCurrentDirectory(CString&szPath)
- {
- TCHARbuf[256]={0};
- GetModuleFileName(NULL,buf,sizeof(buf)/sizeof(TCHAR));
- szPath=buf;
- szPath=szPath.Left(szPath.ReverseFind('\\')+1);
- }
- CStringstrDbPath;
- GetCurrentDirectory(strDbPath);
- strDbPath+=sqlITE3_FILE_NAME;
- Cppsqlite3DBdb;
- try
- //打开或新建一个数据库
- db.open(strDbPath);
- //判断表名是否已经存在
- if(!db.tableExists(TEXT("Customers")))
- //不存在,新建表Customers
- db.execDML(TEXT("CREATETABLECustomers(cust_namevarchar(50)NOTNULLPRIMARYKEY,cust_addressvarchar(50));"));
- //插入1条记录
- db.execDML(TEXT("INSERTINTOCustomersVALUES('VillageToys','200MapleLane');"));
- db.execDML(TEXT("INSERTINTOCustomersVALUES('KidsPlace','333SouthLakeDrive');"));
- //删除1条记录
- db.execDML(TEXT("DELETEFROMCustomersWHEREcust_name='VillageToys';"));
- //使用显示事务插入10条记录
- TCHARbuf[256]={0};
- db.execDML(TEXT("BEGINTRANSACTION;"));
- for(inti=0;i<10;++i)
- {
- memset(buf,153); background-color:inherit; font-weight:bold">sizeof(buf));
- wsprintf(buf,TEXT("INSERTINTOCustomersVALUES('Fun%dALL','%dSunnyPlace');"),i,i);
- db.execDML(buf);
- db.execDML(TEXT("COMMITTRANSACTION;"));
- //更新1条记录
- db.execDML(TEXT("UPDATECustomeRSSETcust_address='454553rdStreet'WHEREcust_name='Fun0ALL';"));
- //获取总记录条数
- intnCount=db.execScalar(TEXT("SELECTCOUNT(*)FROMCustomers;"));
- TCHARszCount[50]={0};
- memset(szCount,153); background-color:inherit; font-weight:bold">sizeof(szCount));
- wsprintf(szCount,TEXT("Recordcount:%d."),nCount);
- AfxMessageBox(szCount);
- //获取每一条记录
- Cppsqlite3Queryq=db.execQuery(TEXT("SELECT*FROMCustomers;"));
- while(!q.eof())
- AfxMessageBox(q.fieldValue(0));
- q.nextRow();
- }
- //销毁语句
- q.finalize();
- //关闭数据库
- db.close();
- AfxMessageBox(TEXT("测试完成!"));
- catch(Cppsqlite3Exceptionex)
- AfxMessageBox(ex.errorMessage());
- }
(4)
Cppsqlite3U封装了4个类:Cppsqlite3Exception、Cppsqlite3DB、Cppsqlite3Statement、Cppsqlite3Query。
a)Cppsqlite3Exception用于捕捉异常,errorCode以整数类形返回错误码,errorMessage以Unicode字符串类型返回错误码。
@H_744_502@copy- classCppsqlite3Exception
- public:
- ……
- constinterrorCode(){returnmnErrCode;}
- LPCTSTRerrorMessage(){returnmpszErrMess;}
- staticLPCTSTRerrorCodeAsString(intnErrCode);
- };
通常用法如:
@H_744_502@copy- try
- ……
- catch(Cppsqlite3Exceptionex)
- b)Cppsqlite3DB用于新建数据库,打开关闭数据库连接,执行DML、DDL,检索数据等。如:open打开数据库连接,close关闭数据库连接,tableExists检查某表是否存在,execDML执行sql命令,execQuery检索记录,setBusyTimeout设置sqlite内部的busy handler的超时时间,sqliteVersion返回sqlite版本。
@H_744_502@copy
@H_404_92@ @H_404_92@ @H_404_92@
- classCppsqlite3DB
- voidopen(LPCTSTRszFile);
- voidclose();
- booltableExists(LPCTSTRszTable);
- intexecDML(LPCTSTRszsql);
- Cppsqlite3QueryexecQuery(LPCTSTRszsql);
- intexecScalar( CStringexecScalarStr( Cppsqlite3StatementcompileStatement( sqlite_int64lastRowId();
- voidinterrupt(){sqlite3_interrupt(mpDB);}
- voidsetBusyTimeout(intnMillisecs);
- staticchar*sqliteVersion(){returnsqlITE_VERSION;}
- };
c)Cppsqlite3Statement也可以执行sql命令,它最大的特点是支持参数绑定。对于参数绑定的用处,参考“SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记”学习。该类导出的接口函数中,bind用于给sql语句中的参数绑定指定的值,reset函数用于重置sql语句,finalize用于销毁语句。
@H_744_502@copy@H_404_92@ @H_404_92@ @H_404_92@- classCppsqlite3Statement
- Cppsqlite3Statement&operator=(constCppsqlite3Statement&rStatement);
- intexecDML();
- Cppsqlite3QueryexecQuery();
- voidbind(intnParam,LPCTSTRszValue);
- intnValue);
- doubledwValue);
- constunsignedchar*blobValue,87); background-color:inherit; font-weight:bold">intnLen);
- voidbindNull(intnParam);
- voidreset();
- voidfinalize();
- 用法如:
@H_744_502@copy
@H_404_92@ @H_404_92@ @H_404_92@
- Cppsqlite3DBdb;
- db.execDML("CREATETABLEemp(empnoint,empnamechar(20));");
- db.execDML("BEGINTRANSACTION;");
- Cppsqlite3Statementstmt=db.compileStatement("INSERTINTOempVALUES(:empno,:empname);");
- for(i=0;i<nRowsToCreate;++i)
- charbuf[16];
- sprintf(buf,"EmpName%06d",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> stmt.bind(":empno",i);
- stmt.bind(":empname",buf);
- stmt.execDML();
- stmt.reset();
- db.execDML("COMMITTRANSACTION;");
- catch(Cppsqlite3Exception&e)
- cerr<<e.errorCode()<<":"<<e.errorMessage()<<endl;
- d)Cppsqlite3Query用于检索记录并读出结果。它导出的public接口函数大多都是sqlite3的C API _sqlite3_column_xxx函数的封装,通过该API函数读取结果集中某一行的某一列。nextRow函数用于检索下一行,eof用于判断是否到结果集的结尾。
copy@H_404_92@ @H_404_92@ @H_404_92@
- classCppsqlite3Query
- …….
- intnumFields();
- intfieldIndex(LPCTSTRszField);
- LPCTSTRfieldName(intnCol);
- LPCTSTRfieldDeclType(intnCol);
- intfieldDataType(LPCTSTRfieldValue(intnField);
- LPCTSTRszField);
- intgetIntField(intnField,87); background-color:inherit; font-weight:bold">intnNullValue=0);
- LPCTSTRszField,87); background-color:inherit; font-weight:bold">intnNullValue=0);
- doublegetFloatField(doublefNullValue=0.0);
- doublefNullValue=0.0);
- LPCTSTRgetStringField(LPCTSTRszNullValue=_T(""));
- LPCTSTRszNullValue=_T(""));
- char*getBlobField(int&nLen);
- int&nLen);
- boolfieldIsNull(booleof();
- voidnextRow();
- sqlite3数据库Native C++封装类(Unicode)Cppsqlite3U的初步认识与使用
猜你在找的Sqlite相关文章