#SQLite Callback to Structure

42 messages · Page 1 of 1 (latest)

tired birch
#

hello guys, i just make SQLite Callback to structure, but why the output its like this?

My code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3/sqlite3.h"

typedef struct {
   int totalArray;
   char** azColName;
   char** value;
} SQLiteCallback;

static int callback(void *data, int argc, char **argv, char **azColName) {
    ((SQLiteCallback*)data)->azColName = malloc(argc);
    ((SQLiteCallback*)data)->value = malloc(argc);
    memset(((SQLiteCallback*)data)->azColName, 0, argc);
    memset(((SQLiteCallback*)data)->value, 0, argc);

    ((SQLiteCallback*)data)->totalArray = argc;

    for (int a = 0; a < argc; a++) {
        ((SQLiteCallback*)data)->azColName[a] = malloc(strlen(azColName[a]) + 1);
        ((SQLiteCallback*)data)->value[a] = malloc(strlen(argv[a]) + 1);

        memset(((SQLiteCallback*)data)->azColName[a], 0, strlen(azColName[a]) + 1);
        memset(((SQLiteCallback*)data)->value[a], 0, strlen(argv[a]) + 1);

        memcpy(((SQLiteCallback*)data)->azColName[a], azColName[a], strlen(azColName[a]));
        memcpy(((SQLiteCallback*)data)->value[a], argv[a], strlen(argv[a]));

        printf("(callback) %s: %s\n", ((SQLiteCallback*)data)->azColName[a], ((SQLiteCallback*)data)->value[a]);
    }
    return 1;
}

int main(int argc, char* argv[]) {
    sqlite3 *db;
    SQLiteCallback cb;

    sqlite3_open("test.db", &db);
    sqlite3_exec(db, "SELECT * from COMPANY", callback, &cb, NULL);

    for (int a = 0; a < cb.totalArray; a++) printf("(main) %s: %s\n", cb.azColName[a], cb.value[a]);
    
    sqlite3_close(db);

    return 0;
}
gray sandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

timber hollow
#

why not?

tired birch
#

how do i fix that?

timber hollow
#

fix what?

tired birch
#

how to fix the main?

#

the callback function its working perfectly

#

but, when printing on main function, its like that?

#

how do i fix that?

timber hollow
#

what is that?

tired birch
#

so, i make callback to structure?

#

but, how do i fix at main print?

#

the callback function is working properly, but in main, its different

timber hollow
#

malloc(argc); what does this do?

tired birch
#

argc is total array

#

from sqlite

tired birch
#

or, is SQLite doesnt support callback struct?

#

or because i am using char**?

timber hollow
#

i suspect you have not understood argc

#

and or understood what the value you pass to malloc() means

tired birch
#

so, on argc, its 5, and its correct, it will make 5 arrays

#

and the array, make malloc for string

#

idk it correct or not

timber hollow
#

sounds like you don't understand malloc.

tired birch
#

im sorry

#

but, can malloc to make like array?

#

example

#

char* a[5]; static

timber hollow
tired birch
#

but how do i make dynamic?

tired birch
#

@timber hollow so, the problem in malloc?

timber hollow
#

a problem. don't presume there is only 1

tired birch
#

ohhh okay, thanks

gray sandBOT
#

@tired birch Has your question been resolved? If so, run !solved :)

tired birch
#

@timber hollow Thanks bro, its work

#

the problem is in

cb->azColName = malloc(argc);
cb->value = malloc(argc);
#

to fix this, its must do it like this

cb->azColName = malloc(argc * sizeof(char*));
cb->value = malloc(argc * sizeof(char*));
#

thanks @timber hollow

#

!solve