sqlite3基本操作

    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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值