SQLite3 for WinCE or Mobile (EVC篇)

来源:互联网 发布:怎么看淘宝商家级别 编辑:程序博客网 时间:2024/04/28 20:01

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, 
0sizeof(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, 
0sizeof(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

   下载

原创粉丝点击