jeudi 18 février 2016

Getting String value from a table in Sqlite

I have a table with 3 columns. I need to select a record by unique id and print the other 2 records

Im using

 rs = get_result("SELECT sid FROM sdata WHERE sid='%s';",sid);

Im able to get the resultset-I verified that.

But this line produces an error

 printf(1,0,(unsigned char *)resultset.recordset[0][1],0);

Isn't this the proper way to print records.

UPDATE:

Im developing this for a embedded device and cross compiling against gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-gcc

resultset get_result(const char* fmt, ...)
{
    int success = 0;
    int nrow=0, ncol=0, i=0, j=0, count=0;
    char *err_messg;
    char **result;
    char ***recordset;
    resultset resultset_table;

    char sql_string[1500];//this honestly needs to be more elegant; will do for now
    va_list args;   
    va_start(args, fmt);
    sql_string[0] = '\0';
    ret = vsprintf(sql_string, fmt, args);
    va_end(args);   
    fprintf(stdout,"\n%s\n", sql_string);

    SQLITE = 1;

    //initialize resultset_table;
    resultset_table.rows = 0;
    resultset_table.cols = 0;
    resultset_table.recordset = NULL;

    ret = sqlite3_get_table(
                    db_conn,              
                    sql_string,
                    &result,
                    &nrow,
                    &ncol,
                    &err_messg
                );

    fprintf(stdout,"nrow=%d ncol=%d\n",nrow,ncol);

    recordset = (char ***)malloc(nrow * sizeof(char **));
    for(count=ncol; count<((nrow + 1)*ncol); count++)
    {
        recordset[i] = (char **)malloc(ncol * sizeof(char *));
        for(j=0; j<ncol; j++)
        {
            err_printf("%s ",result[count]);//
            recordset[i][j] = (char *) malloc( (strlen(result[count]) + 1) );
            strcpy(recordset[i][j], result[count]);

            if(j != (ncol - 1))
                count++;
        }
        i++;
        err_printf("\n");//
    }
    sqlite3_free_table(result);

    if( ret != SQLITE_OK )
    {
        fprintf(stdout,"SQL error: %s\n", err_messg);
        success = 0; 
        }

    else
    {
        resultset_table.rows = nrow;
        resultset_table.cols = ncol;
        resultset_table.recordset = recordset;
        success = 1;
    }

    SQLITE = 0;
    return resultset_table;
}

Error: error: expected expression before 'resultset'

Aucun commentaire:

Enregistrer un commentaire