您好!欢迎访问西安橄榄石信息科技 专注网站建设 公众号 小程序 信息系统 app 开发!
专注 网站建设,信息系统开发丰富的行业经验,一流的服务水准。
当前位置:首页 > 论坛 > 万物互联
  • 查看:8183 回复:1
    C/C++分页查询sqllite
  • mrg

    注册会员
    注册时间:2016-08-10
    积分:156

    发表于:2017-8-10 21:45:53
    int32_t get_alarmlog_list(alarm_log_info_t **alarm_list, int *num, int *total, int page, int rows ,char *sqlquery)
    {
    printf("获取alarm_log信息%d,%d", page, rows);
    printf("\n this is my sqlstr1 %s\n",sqlquery);
    char sqlwher[128];
    sprintf(sqlwher,"%s",sqlquery);
    alarm_log_info_t *alarm_temp;
    //定义
    char * sql[128];
    sprintf(sql, "SELECT * FROM t_alarm_log %s LIMIT %d OFFSET %d ; ",sqlwher, rows, (page-1)*rows);
    printf(sql);
    //char * sql="SELECT * FROM t_alarm_log  LIMIT 0,5  ;";
    char * zErrMessage;
    int nrow = 0; int ncolumn = 0;//
    char** datatempTable;//存放sqlget数据
    //
    char sql2[128];
    sprintf(sql2, "SELECT count(*) FROM t_alarm_log %s;",sqlwher);
    sqlite3 *db;
    sqlite3_open(CAMERA_DB, &db);
    sqlite3_get_table(db, sql, &datatempTable, &nrow, &ncolumn, &zErrMessage);
    //int total2=0;// = sqlite3_exec(db,sql2,NULL,NULL,NULL);
    char  result[32];
    char** datatempTable1;//存放sqlget数据
    char * zErrMessage1;
    int nrow1 = 0; int ncolumn1 = 0;//
    sqlite3_get_table(db, sql2, &datatempTable1, &nrow1, &ncolumn1, &zErrMessage1);
    sqlite3_close(db);
    /*for (int i = 0; i < nrow1; i++)
    {
    printf(datatempTable1[i]);
    }*/

    snprintf(result, "%s", datatempTable1[1]);
    *total = atoi(result);
    printf("   |-%s---总条数%s,-%d-%d----\n", datatempTable1[0], result, total, ncolumn1);
    //动态指定双指针并初始化
    alarm_temp = (alarm_log_info_t*)malloc(nrow * sizeof(alarm_log_info_t));
    memset(alarm_temp, 0, nrow * sizeof(alarm_log_info_t));
    if (!alarm_temp)
    {
    perror("malloc false");
    return-1;
    }
    *alarm_list = alarm_temp;
    *num = nrow;

    //遍历赋值给alarm_temp
    struct tm *timeinfo;
    time_t t;

    for (int i = 0; i < nrow; i++)
    {
    snprintf(alarm_temp[i].dev_id, "%s", datatempTable[(i + 1)*ncolumn + 1]);
    snprintf(alarm_temp[i].dev_name, "%s", datatempTable[(i + 1)*ncolumn + 2]);

    //if (sizeof(datatempTable[(i+1)*ncolumn + 3])>2)
    //{
    // t=atoi("1493706955");
    // timeinfo =localtime(&t);
    // strftime(alarm_temp[i].begin_time, sizeof(alarm_temp[i].begin_time), TIME_STR_FMT, timeinfo);
    // printf ("%s-----\n",t);
    //}

    /* t=(time_t)atoi("1493706955");
    timeinfo =localtime(&t);
    strftime(alarm_temp[i].begin_time, sizeof(alarm_temp[i].begin_time), TIME_STR_FMT, timeinfo);*/
    //printf ("--%s\n",timeinfo);
    //printf("--%s--",datatempTable[(i+1)*ncolumn + 3]);
    char time[32];
    snprintf(time, "%s", datatempTable[(i + 1)*ncolumn + 3]);
    if (0 != strcmp(time, ""))
    {
    t = (time_t)atoi(time);
    timeinfo = localtime(&t);
    strftime(alarm_temp[i].begin_time, sizeof(alarm_temp[i].begin_time), TIME_STR_FMT, timeinfo);
    printf("--%s--\n", time);
    snprintf(time, "%s", "");
    }

    snprintf(time, "%s", datatempTable[(i + 1)*ncolumn + 4]);
    if (0 != strcmp(time, ""))
    {
    t = (time_t)atoi(time);
    timeinfo = localtime(&t);
    strftime(alarm_temp[i].end_time, sizeof(alarm_temp[i].end_time), TIME_STR_FMT, timeinfo);
    printf("--%s--\n", time);
    // printf("--%s--\n",datatempTable[(i+1)*ncolumn + 4]);
    }
    else
    {
    printf("--该行为空%s--\n", alarm_temp[i].end_time);
    }

    snprintf(alarm_temp[i].video_name, "%s", datatempTable[(i + 1)*ncolumn + 5]);
    snprintf(alarm_temp[i].video_path, "%s", datatempTable[(i + 1)*ncolumn + 6]);
    snprintf(alarm_temp[i].picture_name, "%s", datatempTable[(i + 1)*ncolumn + 7]);
    snprintf(alarm_temp[i].picture_path, "%s", datatempTable[(i + 1)*ncolumn + 8]);
    snprintf(alarm_temp[i].type, "%s", datatempTable[(i + 1)*ncolumn + 9]);
    snprintf(alarm_temp[i].remark, "%s", datatempTable[(i + 1)*ncolumn + 10]);
    }


    printf("get alarm_log ok !");
    return 0;

    }

    int alarmlog_free(alarm_log_info_t *ptr){

    if (ptr != NULL)
    {
    free(ptr);
    ptr = NULL;
    }
    return 0;
    };
    发帖IP:117.23…
  • 西安橄榄石信息科技

    注册会员
    注册时间:2017-10-30
    积分:0

    发表于:2017-10-30 21:52:47
    本大侠的c++/c写的挺六六六的,,,,,,
    发帖IP:123.13…
备案号:   陕ICP备17007433号   西安橄榄石信息科技 专注网站建设 公众号 小程序 信息系统 app 开发 城市分站:西安上海北京兰州陕西汉中安康商洛渭南户县咸阳
服务热线:17691183459 17691183459 联系手机:17691183459 传真号码:
联系地址:西安市高新区锦业路软件园 西安市橄榄石信息科技有限责任公司从事西安做网站,西安网站设计,做网站的公司,西安做网站公司,西安建站,西安网站制作,做网站,建站,做网页,微信开发,app制作,app开发,管理系统开发的产品,欢迎前来咨询!