利用VS2005编译SQLite3.5.1
1, 到http://sourceforge.net/projects/sqlite-wince/files/下载 sqlite-wince-3.x下面的sqlite-wince-3.3.5
2. 创建一个Win32 Smart Device Project,项目名字为sqlite3,Application Type选择DLL,创建项目完成后,将所有sqlite-wince-3.3.5下面的文件拷贝到项目的目录(除了tclsqlite.c和shell.c这两个文件)
3. 设置project的一些属性,
首先将sqlite3.cpp改名为sqlite.c,并注释掉整个文件里面的代码,包括include的头文件,因为我们待会儿会按照C语言来编译,而不是C++,如果按照C++编译,会有很多语法错误,当然,你也可以逐条修改语法错误,然后编译(这个我没试过,应该没问题);另外,从项目中删掉stdafx.cpp,这个文件没用。
其次,将编译模式改为c模式(TC)。位置:Project->Properties->Configuration
Properties->C/C++->Advanced->Compile
As
再次,Project->Properties->C/C++->Precompiled Header->Create/Use Precompiled Header: Not using precompiled Header
再次,Project->Properties->Linker->Input-> Module Definition File: 输入“sqlite3.def”
再次,有可能会出现unresolved external symbol @__security_check_cookie@4这个链接错误,做如下操作即可:Project->Properties->C/C++->Code Generation, Buffer Security Check选择No
上述过程完成后即可得到一个sqlite3.lib和sqlite3.dll
4. 如果想使用c++封装,可以到下面下载: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; { //打开或新建一个数据库 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(); } catch(CppSQLite3Exception ex) { AfxMessageBox(ex.errorMessage()); } }
|