C++访问sqlite3实践
来源:互联网 发布:苹果安装ps软件 编辑:程序博客网 时间:2024/06/05 06:25
Sqlite确实是一个比较好的本地数据库,从接触它的时候就喜欢上了它,它可以在很多情况下简化应用。不过以前都是在Java里面使用,或者Linux C下使用的,现在有个项目(C++)可能我会用到sqlite做数据持久化,所以先热热身。
第一步:下载相关文件
首先到这里下载sqlite-source-3_6_12.zip、sqlite-3_6_12.zip、 sqlitedll-3_6_12.zip三个包,并分别解压。
第二步:生成SQLite的lib文件
cmd进入命令行后输入: LIB /DEF:SQLITE3.DEF /MACHINE:IX86
如果找不到命令LIB,则需要将Microsoft Visual Studio\VC98\Bin这个目录添加到环境变量里。这样就生成了sqlite3.lib文件,我们在后面需要用到这个库,用于链接win32程序
新建项目,将sqlite3.h(在源码包里)、sqlite3.dll、sqlite3.lib设置到工程环境里,或者直接拷贝到工程目录下。
然后我们将cmd切换到sqlite3的目录下,里面有个sqlite3.exe。执行命令:
> sqlite3 D:\sql.db ;生成sql.db的数据库文件
sqlite3 > create table test_tab (f1 int, f2 long);
sqlite3 > .q
这样我们就生成了一张test_tab的表。
然后编写如下代码:
编译、链接、执行,看看效果吧。
SQLite不愧是数据存储的 "瑞士军刀".不像使用某些数据库,要配置ODBC,还要把一大堆的dll一起打包到最终的用户程序中去.还得使用depends之类的工具看要打包哪些.dll.
第一步:下载相关文件
首先到这里下载sqlite-source-3_6_12.zip、sqlite-3_6_12.zip、 sqlitedll-3_6_12.zip三个包,并分别解压。
第二步:生成SQLite的lib文件
cmd进入命令行后输入: LIB /DEF:SQLITE3.DEF /MACHINE:IX86
如果找不到命令LIB,则需要将Microsoft Visual Studio\VC98\Bin这个目录添加到环境变量里。这样就生成了sqlite3.lib文件,我们在后面需要用到这个库,用于链接win32程序
如果设置了环境变量,利用LIB仍不能编译成功,可把SQLITE3.DEF,sqlite3.dll拷贝到VS对应的Bin下,来执行Lib命令。我的开发环境是Win7 64,VS2010.
在C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 路径下lib成功。
第三步:编写测试工程新建项目,将sqlite3.h(在源码包里)、sqlite3.dll、sqlite3.lib设置到工程环境里,或者直接拷贝到工程目录下。
然后我们将cmd切换到sqlite3的目录下,里面有个sqlite3.exe。执行命令:
> sqlite3 D:\sql.db ;生成sql.db的数据库文件
sqlite3 > create table test_tab (f1 int, f2 long);
sqlite3 > .q
这样我们就生成了一张test_tab的表。
然后编写如下代码:
- #include "sqlite3.h"
- #include <iostream>
- #include <sstream>
- using namespace std;
- sqlite3 * pDB;
- int createTable()
- {
- char* errMsg;
- std::string dropTab = "drop table test_tab;";
- string strSQL= "create table test_tab (f1 int, f2 long);";
- int res= sqlite3_exec(pDB , dropTab.c_str() , 0 , 0 , &errMsg);
- if (res != SQLITE_OK)
- {
- std::cout << "执行SQL 出错." << errMsg << std::endl;
- return -1;
- }
- res = sqlite3_exec(pDB , strSQL.c_str() ,0 ,0, &errMsg);
- if (res != SQLITE_OK)
- {
- std::cout << "执行创建table的SQL 出错." << errMsg << std::endl;
- return -1;
- }
- else
- {
- std::cout << "创建table的SQL成功执行."<< std::endl;
- }
- return 0;
- }
- int insert1()
- {
- char* errMsg;
- int res = sqlite3_exec(pDB,"begin transaction;",0,0, &errMsg);
- for (int i= 1; i < 10; ++i)
- {
- std::stringstream strsql;
- strsql << "insert into test_tab values(";
- strsql << i << ","<< (i+10) << ");";
- std::string str = strsql.str();
- res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
- if (res != SQLITE_OK)
- {
- std::cout << "执行SQL 出错." << errMsg << std::endl;
- return -1;
- }
- }
- res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);
- std::cout << "SQL成功执行."<< std::endl;
- return 0;
- }
- static int callback(void *NotUsed, int argc, char **argv, char **azColName)
- {
- for(int i = 0 ; i < argc ; i++)
- {
- std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << ", " ;
- }
- std::cout<< "\n";
- return 0;
- }
- int select1()
- {
- char* errMsg;
- string strSQL= "select * from test_tab;";
- int res = sqlite3_exec(pDB, strSQL.c_str(), callback , 0 , &errMsg);
- if (res != SQLITE_OK)
- {
- std::cout << "执行SQL 出错." << errMsg << std::endl;
- return -1;
- }
- else
- {
- std::cout << "SQL成功执行."<< std::endl;
- }
- return 0;
- }
- int main()
- {
- int res = sqlite3_open("D:\\sql.db", &pDB);
- if( res ){
- std::cout << "Can't open database: "<< sqlite3_errmsg(pDB);
- sqlite3_close(pDB);
- return -1;
- }
- res = createTable();
- if (res != 0)
- {
- return 0;
- }
- res = insert1();
- if (res != 0)
- {
- return 0;
- }
- select1();
- return 0;
- }
编译、链接、执行,看看效果吧。
SQLite不愧是数据存储的 "瑞士军刀".不像使用某些数据库,要配置ODBC,还要把一大堆的dll一起打包到最终的用户程序中去.还得使用depends之类的工具看要打包哪些.dll.
更多学习参看sqlite提供的document啦```
第四步,测试通过了,如果要应用的项目里,使用sqlite3提供的借口,不是很方便,需要再封装一层。推荐一个不错的 开源的wapper:SQLiteCpp.
地址:https://github.com/SRombauts/SQLiteCpp
SQLiteC++ (SQLiteCpp) is a smart and easy to use C++ SQLite3 wrapper.
0 0
- C++访问sqlite3实践
- C++访问sqlite3实践
- C/C++ 操控访问SQLite3 数据库
- C+sqlite3
- Sqlite3学习实践总结
- Linux下C访问MySQL实践
- 访问sqlite3数据库
- nodejs访问sqlite3
- iphone Sqlite3学习实践总结
- iOS开发实践之SQLite3
- 使用 SQLite3 访问Blob字段
- 使用 SQLite3 访问Blob字段
- (转)SQLite3 多线程访问
- SQLite3的C编程
- SQLite3 -- C 编程
- Linux C SQLite3 编程
- Linux C SQLite3 编程
- SQLITE3,C语言使用
- HMAC-SHA1加密,OAUTH中签名时使用的一种方法。
- vs2010环境下提示找不到d3dx9.h
- Android 开发之底层驱动开发(二)
- 枚举—Swift学习笔记(十一)
- node学习1
- C++访问sqlite3实践
- 不确定,是科学唯一确定的属性
- listview下拉刷新、上拉加载,无法刷新问题解决方法
- IT学吧C++俄罗斯方块游戏开发
- 《.net设计规范--.net约定、惯用法与模式》读书笔记
- cocos2d-x-lua基础系列教程二(lua内存管理)
- Golang Multipart File Upload Example
- .NET程序员必备工具
- 知道得越多,越难改变观点