sqlite基本操作补充

string path = MGBundle::mainBundle()->userFilePathWithFileName("brush_data.db");
    sqlite3 *pDB;
    char *errMsg;
    if(!Utility::isFileORDirExist(path))
    {
        int res = sqlite3_open(path.c_str(), &pDB);
        if(res != SQLITE_OK)
        {
            MGLogD("brushdata db open fail");
            sqlite3_close(pDB);
            return;
        }
        string sql = "create table brush_data(white text, wounded text, kiss text, date_str 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("brushdata db open fail");
        sqlite3_close(pDB);
        return;
    }
//数据库表条件查询
    int row = 0, column = 0;
    char **result;
    string sql = "select * from brush_data order by date_str desc,date_str limit 0,1;";
    res = sqlite3_get_table(pDB, sql.c_str(), &result, &row, &column, &errMsg);
    int i = 0;
    for(i = column; i < (row + 1) * column; i ++)
        MGLogD("reslut:%s", result[i]);
    sqlite3_free_table(result);
    sqlite3_close(pDB);
string path = MGBundle::mainBundle()->userFilePathWithFileName("brush_data.db");
    sqlite3 *pDB;
    char *errMsg;
    if(!Utility::isFileORDirExist(path))
    {
        int res = sqlite3_open(path.c_str(), &pDB);
        if(res != SQLITE_OK)
        {
            MGLogD("brushdata db open fail");
            sqlite3_close(pDB);
            return;
        }
        string sql = "create table brush_data(white text, wounded text, kiss text, date_str 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("brushdata db open fail");
        sqlite3_close(pDB);
        return;
    }
//数据表记录删除和插入
	string tmp = TimeState::getCurrentTime().getTimeString("111000");
	for(auto it = v_syn_records.begin(); it != v_syn_records.end(); ++ it)
	{
		string sql = "delete from brush_data where date_str like '" + (*it).date_time.getTimeString("111000");
		sql += "%';";
		res = sqlite3_exec(pDB, sql.c_str(), 0, 0, &errMsg);
		string w_value = Utility::BuiltInTypeToString<int>((*it).white_value);
		string wo_value = Utility::BuiltInTypeToString<int>((*it).wounded_value);
		string k_value = Utility::BuiltInTypeToString<int>((*it).kiss_value);
		string d_value = (*it).date_time.getTimeString("111111");
		sql = "insert into brush_data values('" + w_value +"','"+ wo_value + "','"+ k_value + "','"+ d_value + "');";
		res = sqlite3_exec(pDB, sql.c_str(), 0, 0, &errMsg);
	}    
    sqlite3_close(pDB);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值