这篇文章,主要介绍的是:利用网上封装好操作sqlite3的类CppSQLite3DB,来进行数据的操作。由于源码是开放的,所以可以很清楚的知道执行open操作时,到底用了哪些Sqlite3接口,这里不再做详细介绍。
1、下载CppSQLite3.h和CppSQLite3.cpp这个封装好的源码文件;
2、下载Sqlite3文件,不知道的可以网上搜索下,主要有sqlite3.dll,sqlite3.h文件,最重要的还有个sqlite3.lib文件这个文件如果没有可以通过sqlite3.dll文件直接进行生成,网上有相应的教程可参考;
3、将Sqlite3.lib和CppSQLite3.h和.cpp文件同样放到工程的目录下,然后在vs上添加此文件;
4、打开CppSQLite3.cpp修改下#pragma comment(lib, "Sqlite3.lib"),把lib对应的名称改成你导入的sqlite3.lib;
至此,通过CppSQLite3操作sqlite工程环境已经搭建完毕。
首先,声明个CppSQLite3DB的指针对象:CppSQLite3DB* pSqlite = new CppSQLite3DB;
打开数据库操作:void CppSQLite3DB::open(const char* szFile),注意有的CppSQLite3源码并没有对szFile进行UTF-8转码,所以在外部调用时还需要将待传入的db绝对路径进行UTF-8转码,提供个函数
std::string GBKToUTF8(const std::string& strGBK)
{
std::string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}
执行sql语句:int CppSQLite3DB::execDML(const char* szSQL);
执行查询语句,并逐条读取结果:CppSQLite3Query CppSQLite3DB::execQuery(const char* szSQL)
//!<执行到这表file_info已经存在
//!<从表file_info读取file_status=1(1 密文文件),每次读取50条数据,保存到链表中
CStringList strFilePathList; //!<保存文件绝对路径信息链表
CString strPath = _T("");
CppSQLite3Query result = g_sqlieDriveDB->execQuery("select file_path from file_info where \
file_status = 1 limit 50;");
while(!result.eof()) //!<遍历获取的数据
{
strPath = A2W(result.fieldValue(0));
theApp.m_Log.AddMsgLog(strPath);
strFilePathList.AddTail(strPath);
result.nextRow();
}
//!<释放资源防止死锁
result.finalize();
...
还有很多接口,等大家慢慢去用,暂时介绍以上几个经常用的
放上csdn下载地址,可以下载到CppSQLite3所需的源码
http://download.csdn.net/detail/fzuim/9836428