SQLite3 for WinCE or Mobile (EVC篇)

在WinCE,Mobile上,对sqlite的开发,目前还是以.net compact framework的封装居多.

http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 可找到各种语言对 sqlite 的封装.

下面将介绍如何在EVC下使用sqlite.

1> 开发工具: EVC4.0 + SP2

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/


4> 有了 sqlite DLL 及 Cppsqlite3U 后,便可以很方便地使用 sqlITE :(步骤3中,a链接页画下就有DEMO)

主要代码如下:

#define FILE_DB_NAME TEXT("unitech.db")

//获取程序当前路径
void GetCurrentDirectory(CString &szPath)
{
wchar_t pBuf[
256];
GetmodulefileName(NULL,pBuf,
sizeof(pBuf)/sizeof(wchar_t));
szPath
=pBuf;
szPath
= szPath.Left(szPath.ReverseFind('\\')+1);
}

void CDemo2Dlg::OnButton1()
{
CString strDbPath;
GetCurrentDirectory(strDbPath);
strDbPath
+= FILE_DB_NAME;

Cppsqlite3DB db;
try
{
//打开或新建一个数据库
db.open(strDbPath);

//判断表名是否存在
if(db.tableExists(L"tblTest"))
{
AfxMessageBox(L
"Table: tblTest is existed!");
}
else //不存在
{
AfxMessageBox(L
"Table: tblTest not existed!");
//新建表
db.execDML(L"create table tblTest(empno varchar(20),empname varchar(20))");
}

//插入一笔记录
db.execDML(L"insert into tblTest values('编号','姓名')");
//插入一笔记录
db.execDML(L"insert into tblTest values('精瑞电脑','Answer')");

//删除一笔记录
db.execDML(L"delete from tblTest where empno='编号'");

//插入10笔记录(使用事务)
TCHAR buf[256];
db.execDML(L
"begin transaction;");
for (int i = 0; i < 10; i++)
{
memset(buf,
0,sizeof(buf));
wsprintf(buf,L
"insert into tblTest values ('no%d','name%d');",i,i);
db.execDML(buf);
}
db.execDML(L
"commit transaction;");

//更新一笔记录
db.execDML(L"update tblTest set empname='answer' where empno='no1'");

//获取总笔数
int count = db.execScalar(L"select count(*) from tblTest;");
TCHAR szCount[
50];
memset(szCount,
sizeof(szCount));
wsprintf(szCount,L
"Count:%d",count);
AfxMessageBox(szCount);

//获取每一笔
Cppsqlite3Query q = db.execQuery(L"select * from tblTest");
while (!q.eof())
{
AfxMessageBox(q.fieldValue(
0));
q.nextRow();
}
q.finalize();

db.close();
AfxMessageBox(L
"OK");
}
catch(Cppsqlite3Exception ex)
{
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

下载


【本文转载于

http://www.cnblogs.com/answer/archive/2008/09/12/1289681.html

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能