sqlite3基本操作
2016-01-02 21:48
351 查看
string path = "device.db"; sqlite3 *pDB; sqlite3_stmt *stmt; char *errMsg;
if(!Utility::isFileORDirExist(path))
{//判断数据库文件是否已经存在,不存在是创建数据库和数据库表 int res = sqlite3_open(path.c_str(), &pDB); if(res != SQLITE_OK) { MGLogD("device db open fail"); sqlite3_close(pDB); return; } string sql = "create table device(name text, address text);"; res = sqlite3_exec(pDB, sql.c_str(), 0, 0, &errMsg); sqlite3_close(pDB); }
//打开数据库 int res = sqlite3_open(path.c_str(), &pDB); if(res != SQLITE_OK) { MGLogD("device db open fail"); sqlite3_close(pDB); return; }
//数据库插入操作 string name = "Test01"; string address = "23:3d:34:8f:89"; string np1 = "insert into device values('" + name +"','"+ address + "');"; name = "Test02"; address = "23:3d:34:8d:89"; string np2= "insert into device values('" + name +"','"+ address + "');"; res = sqlite3_exec(pDB, np1.c_str(),0,0, &errMsg); res = sqlite3_exec(pDB, np2.c_str(),0,0, &errMsg); res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);
//数据库查询 int row = 0, column = 0; char **result; string sql = "select * from device;"; res = sqlite3_get_table(pDB, sql.c_str(), &result, &row, &column, &errMsg); int i = 0; for(i = column; i < (row + 1) * column; i ++) cout << result[i]; sqlite3_free_table(result); sqlite3_close(pDB);
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- 初识SQLITE3数据库
- C++联合体转换成C#结构的实现方法
- 将 Ghost 从 SQLite3 数据库迁移到 MySQL 数据库
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例