您的回调,以便跟踪分配的维度。例如,类似这样的事情: struct mytable { char **data; size_t dim; }; SQLCONTROL_API char** sql_execQuery(char *dbNam

您的回调,以便跟踪分配的维度。例如,类似这样的事情: struct mytable { char **data; size_t dim; }; SQLCONTROL_API char** sql_execQuery(char *dbNam,c,arrays,sqlite,multidimensional-array,realloc,C,Arrays,Sqlite,Multidimensional Array,Realloc,您的回调,以便跟踪分配的维度。例如,类似这样的事情: struct mytable { char **data; size_t dim; }; SQLCONTROL_API char** sql_execQuery(char *dbName, char *sqlStatement) { struct mytable a = { NULL, 0 }; // ... int rc = sqlite3_exec(db, sqlStatement, call

您的回调,以便跟踪分配的维度。例如,类似这样的事情:

struct mytable {
    char **data;
    size_t dim;
};

SQLCONTROL_API char** sql_execQuery(char *dbName, char *sqlStatement)
{
    struct mytable a = { NULL, 0 };

    // ...

    int rc = sqlite3_exec(db, sqlStatement, callback, &a, &zErrMsg);

    return a.data;
}

static int callback(void *data, int argc, char **argv, char **azColName)
{
    struct mytable *old = data;
    char **temp;

    old->dim++;
    temp = realloc(old->data, old->dim * sizeof(*old->data));
    if (temp) {
        old->data = temp;
        old->data[old->dim - 1] = NULL;
    } else {
        // handle allocation error ...
    }

    for (int i = 0; i < old->dim; i++) {
        char *temp2 = realloc(old->data[i], argc * sizeof(*old->data[i]));

        if (temp2) {
            old->data[i] = temp2;
            old->data[i][argc - 1] = NULL;
        } else {
            // handle allocation error ...
        }
    }

    // ... other stuff ...

    // no need for anything like data = old

    return 0;
}
struct mytable{
字符**数据;
尺寸变暗;
};
SQLCONTROL\u API char**sql\u execQuery(char*dbName,char*sqlStatement)
{
结构mytable a={NULL,0};
// ...
int rc=sqlite3_exec(db、sqlStatement、callback、&a和zErrMsg);
返回a.data;
}
静态int回调(void*data,int argc,char**argv,char**azColName)
{
struct mytable*old=数据;
炭**温度;
旧->暗++;
temp=realloc(旧->数据,旧->尺寸*sizeof(*旧->数据));
如果(临时){
旧->数据=温度;
旧->数据[旧->尺寸-1]=空;
}否则{
//处理分配错误。。。
}
对于(int i=0;idim;i++){
char*temp2=realloc(旧->数据[i],argc*sizeof(*旧->数据[i]);
如果(临时2){
旧->数据[i]=temp2;
旧->数据[i][argc-1]=NULL;
}否则{
//处理分配错误。。。
}
}
//…其他东西。。。
//不需要像data=old这样的东西
返回0;
}

为什么您试图使用
sqlite3\u exec()
而不是游标界面?@CL。既然
sqlite3\u exec()
是一个包装函数,我想我会比实现每个步骤获得更好更快的编码结果。为什么您试图使用
sqlite3\u exec()
而不是游标界面?@CL。由于
sqlite3_exec()
是一个包装函数,我想我会获得比实现每个步骤更好更快的编码结果。谢谢你的回答。在你的帮助下,我获得了每一列的第一个字符。我试图从
char**argv
数组中获取完整的
char*
字符串。我是否需要一个3d数组,比如
char***temp
?@Strizzi,根据您修改的代码,这听起来像是一个单独的问题。我已经创建了一个。谢谢您的回答。在你的帮助下,我获得了每一列的第一个字符。我试图从
char**argv
数组中获取完整的
char*
字符串。我是否需要一个3d数组,比如
char***temp
?@Strizzi,根据你修改过的代码,这听起来像是一个单独的问题。我已经创建了一个。
static int callback(void *data, int argc, char **argv, char **azColName)
{
    char **old = (char **)data;
    int num_rows = sizeof(old) / sizeof(old[0]);
    int num_cols = sizeof(old[0]) / sizeof(old[0][0]);

    old = (char **)realloc(old, (num_rows + 1) * sizeof(char *));
    for (int i = 0; i < (num_rows + 1); i++)
        old[i] = (char *)realloc(old[i], argc * sizeof(char *));

    /*I am trying to create a 2 dim array that looks like a table, 
    so the column names are in the first row, 
    then the data from the table is stored in each row*/
    for (int i = 0; i < argc; i++)
    {
        if (num_rows == 1)
            old[0][i] = *azColName[i];

        old[num_rows][i] = *argv[i];
    }
    data = old;
    return 0;
}
char **old = *(char ***)data;

// ...

*(char ***)data = old;
struct mytable {
    char **data;
    size_t dim;
};

SQLCONTROL_API char** sql_execQuery(char *dbName, char *sqlStatement)
{
    struct mytable a = { NULL, 0 };

    // ...

    int rc = sqlite3_exec(db, sqlStatement, callback, &a, &zErrMsg);

    return a.data;
}

static int callback(void *data, int argc, char **argv, char **azColName)
{
    struct mytable *old = data;
    char **temp;

    old->dim++;
    temp = realloc(old->data, old->dim * sizeof(*old->data));
    if (temp) {
        old->data = temp;
        old->data[old->dim - 1] = NULL;
    } else {
        // handle allocation error ...
    }

    for (int i = 0; i < old->dim; i++) {
        char *temp2 = realloc(old->data[i], argc * sizeof(*old->data[i]));

        if (temp2) {
            old->data[i] = temp2;
            old->data[i][argc - 1] = NULL;
        } else {
            // handle allocation error ...
        }
    }

    // ... other stuff ...

    // no need for anything like data = old

    return 0;
}