-1

:D 我正在阅读 sqlite 的权威指南(欧文斯,艾伦),并决定实际尝试这本好书中的一些代码,并遇到了一些编译问题。我希望众筹其中的一些问题,同时学习一些 C 和 SQLite!哇是的!

我在编译时收到以下神秘的编译错误:gcc mo.c -om -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
4

1 回答 1

0

您得到的错误是由于 1. nrowsncols未声明变量。它们应该被声明为整数。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 */

);

于 2013-03-22T03:59:18.970 回答