c - SQLite3 连接到 C 程序并使用内部循环迭代结果 - 做什么?

标签 c ubuntu sqlite

: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 *'但参数类型为'字符 * (*)[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

最佳答案

您收到的错误是由于 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 */

);

关于c - SQLite3 连接到 C 程序并使用内部循环迭代结果 - 做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15562326/

相关文章:

c - C 中的管道和 I/O 重定向

c - 有用于 zOS 的 C 调试器吗?

c - 接受通配符以打开文件

c - 加载内核模块时出现未知符号

php - 通过 PHP 从 Ubuntu 连接到远程 Windows Sybase 数据库

Python SQLite3 UPDATE 与元组仅使用最后一个值

c - UDP C 服务器不接收数据包

linux - 无法将第三个参数传递给 bash 脚本

SQL:从 "nothing"中选择一个数字列表

sqlite - 安装 HDBC-SQlite3 Haskell