SQLite3 for WinCE or Mobile (EVC篇)
博客地址http://space.itpub.net/13771794/viewspace-524491 在WinCE,Mobile上,对SQLite的开发,目前还是以.net compact framework的封装居多. 在http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers可找到各种语言对 SQLite 的封装. 下面将介绍如何在EVC下使用SQLite. 2> 编译出所需的 SQLite DLL. a> 在http://sqlite-wince.sourceforge.net/中下载 SQLite for Windows CE 的DLL 源代码. b). 打开eVC新建一个“WCE Dynamic-Link Library”工程,命名为:sqlite3 c). 在接下来的对话框中选择"An empty Windows CE DLL project",点击 FINISH,之后再点击 OK d). 将源码中所有的 *.c *.h *.def 复制到工程文件夹下 e). 在 Source Files 中添加除shell.c和tclsqlite.c这两个文件以外所有 *.c 的SQLite源文件文件 f). 在 Header Files 中添加所有 *.h 的SQLite源文件文件 g). 将 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中 h). 在eVC中选好你要编译的平台,例如“Win32(WCE ARMV4I) Release” i). 好了,开始编译,Build(F7)一下 3> 编译出DLL后,需要使用C++对DLL中的功能进行封装.有如下资源可参考: a>http://www.codeproject.com/KB/database/CppSQLite.aspx b>http://www.adp-gmbh.ch/sqlite/wrapper.html 如上 a,b 资源,尽管已对 SQLite Dll 中的功能进行封装,然而 WinCE,Mobile上使用的是UNICODE编码,而 a,b 却并未支持UNICODE.所以真正要用到的是 a 资源中的 unicode 版本,如下: http://softvoile.com/development/CppSQLite3U/
主要代码如下: #define FILE_DB_NAMETEXT("unitech.db")// 获取程序当前路径 void GetCurrentDirectory(CString & szPath) { wchar_tpBuf[ 256 ]; GetModuleFileName(NULL,pBuf, sizeof (pBuf) / sizeof (wchar_t)); szPath = pBuf; szPath = szPath.Left(szPath.ReverseFind( ' // ' ) + 1 ); } void CDemo2Dlg::OnButton1() { CStringstrDbPath; GetCurrentDirectory(strDbPath); strDbPath += FILE_DB_NAME; CppSQLite3DBdb; try { // 打开或新建一个数据库 db.open(strDbPath); // 判断表名是否存在 if (db.tableExists(L " tblTest " )) { AfxMessageBox(L " Table:tblTestisexisted! " ); } else // 不存在 { AfxMessageBox(L " Table:tblTestnotexisted! " ); // 新建表 db.execDML(L " createtabletblTest(empnovarchar(20),empnamevarchar(20)) " ); } // 插入一笔记录 db.execDML(L " insertintotblTestvalues('编号','姓名') " ); // 插入一笔记录 db.execDML(L " insertintotblTestvalues('精瑞电脑','Answer') " ); // 删除一笔记录 db.execDML(L " deletefromtblTestwhereempno='编号' " ); // 插入10笔记录(使用事务) TCHARbuf[ 256 ]; db.execDML(L " begintransaction; " ); for ( int i = 0 ;i < 10 ;i ++ ) { memset(buf, 0 , sizeof (buf)); wsprintf(buf,L " insertintotblTestvalues('no%d','name%d'); " ,i,i); db.execDML(buf); } db.execDML(L " committransaction; " ); // 更新一笔记录 db.execDML(L " updatetblTestsetempname='answer'whereempno='no1' " ); // 获取总笔数 int count = db.execScalar(L " selectcount(*)fromtblTest; " ); TCHARszCount[ 50 ]; memset(szCount, sizeof (szCount)); wsprintf(szCount,L " Count:%d " ,count); AfxMessageBox(szCount); // 获取每一笔 CppSQLite3Queryq = db.execQuery(L " select*fromtblTest " ); while ( ! q.eof()) { AfxMessageBox(q.fieldValue( 0 )); q.nextRow(); } q.finalize(); db.close(); AfxMessageBox(L " OK " ); } catch (CppSQLite3Exceptionex) { AfxMessageBox(ex.errorMessage()); } } 5> 成功完成,Enjoy it~~~ 6> 下载: a>SQLite3 For WinCE Source b>CppSQLite3U For WinCE Source c>Demo Source d>Demo Exe 7> 推荐一个SQLite的可视化工具: SQLiteSpy:http://www.yunqa.de/delphi/doku.php/products/sqlitespy/index 下载 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |