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

下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章