【发布时间】:2017-02-27 00:29:24
【问题描述】:
我正在开发一个用于教育目的的 sqlite-.dll。 每次使用数据库中的新行调用回调函数时,我都尝试在二维数组中动态添加一行。 (例如,从客户那里选择 *)。 然后应将存储在此数组中的数据作为 C 接口返回。
SQLCONTROL_API char** sql_execQuery(char *dbName, char *sqlStatement)
{
char **a = 0;
/*Some sqlite stuff*/
int rc = sqlite3_exec(db, sqlStatement, callback, &a, &zErrMsg);
return a;
}
带有回调函数:
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;
}
将数据插入数据库时,一切正常。但是当我尝试检索数据时,我会遇到读取访问冲突。 现在我的问题是,我的方法是否正确,还是我错过了一些重要的意图要求?
【问题讨论】:
-
你为什么要使用
sqlite3_exec()而不是光标界面? -
@CL。由于
sqlite3_exec()是一个包装函数,我认为我会比实现每个步骤获得更好更快的编码结果。
标签: c arrays sqlite multidimensional-array realloc