:d 我正在阅读sqlite(owens,allen)的权威指南,并决定在这本精美的书中试验一些代码并遇到一些编译问题。我希望能够挤出一些这些问题并同时学习一些C和SQLite!哇呀!
编译时我收到以下神秘的编译错误: gcc mo.c -o m -lsqlite3
mo.c: In function ‘main’:
mo.c:22:44: error: ‘nrows’ undeclared (first use in this function)
mo.c:22:44: note: each undeclared identifier is reported only once for each function
it appears in
mo.c:22:52: error: ‘ncols’ undeclared (first use in this function)
mo.c:22:2: warning: passing argument 3 of ‘sqlite3_get_table’ from incompatible pointer
type [enabled by default]
/usr/local/include/sqlite3.h:2084:16: note: expected ‘char ***’ but argument is of
type ‘char * (*)[50]’
我最为困惑的是“警告:从不兼容的指针类型[默认启用]传递'sqlite3_get_table'的参数3”和“注意:预期'char * '但参数类型为' char *(*)[50]'
有问题的程序如下:你能不能实现stackoverflow? < ================================================ ===================================>
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
int main()
{
sqlite3 *db;
char *zErr;
int rc;
char *sql;
char *result[50];
int j=0;
int i=0;
rc = sqlite3_open("foods-backup.db", &db);
if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_get_table(db, sql, &result, &nrows, &ncols, &zErr);
for(i; i < nrows; i++) {
for(j; j < ncols; j++) {
/* the i+1 term skips over the first record,
which is the column headers */
fprintf(stdout, "%s", result[(i+1)*ncols + j]);
}
}
if(rc != SQLITE_OK) {
if (zErr != NULL) {
fprintf(stderr, "SQL error: %s\n", zErr);
sqlite3_free(zErr);
}
}
/* Free memory */
sqlite3_free_table(result);
// close database connection or something and return everything is okay i guess
sqlite3_close(db);
return 0;
} // end main
答案 0 :(得分:0)
你得到的错误是因为
1. nrows
,ncols
变量未声明。它们应该被声明为整数。
2. result
变量应声明为char **
而不是char *result[50]
。
因此注释掉你的结果数组指针声明并复制到两行以下。希望这会有所帮助。
int nrows, ncols;
char **result;
http://www.sqlite.org/c3ref/free_table.html
int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);