【问题标题】:SQLite C API, can open DB but not returning any data with Query?SQLite C API,可以打开数据库但不能使用查询返回任何数据?
【发布时间】:2011-09-22 20:24:06
【问题描述】:

我正在尝试使用 SQLite 的 C API 打开数据库并访问一些记录。似乎可以很好地打开数据库,但是在尝试查询时我没有得到任何结果(返回 0 行)。相同的查询适用于 sqlite3 CLI 和其他 sqlite 应用程序/前端。我使用 [http://www.sqlite.org/cintro.html] 作为参考。

我的代码几乎只是两个函数调用。程序输出为:

“打开的数据库”

“行数 = 0”。

我在这里没有做任何复杂的事情,所以我希望我只是在我看不到的地方犯了一个简单的错误。如有任何帮助,我将不胜感激。

-kf

int main()
{
// declare some vars
sqlite3* db_handle;
char sql[2048];     // for commands,queries and such
char* err_msg;
char** results;
int rows, columns;

// connect to database file
if (!(sqlite3_open_v2("../../Downloads/maps/ontario.sql", &db_handle, SQLITE_OPEN_READONLY, NULL)))
{
    std::cerr << "Could not connect to DB" << std::endl;
    std::cerr << "Error: " << sqlite3_errmsg(db_handle);
    sqlite3_close(db_handle);
    return -1;
}
else
{  std::cerr << "Opened DB" << std::endl;  }

// try sending query
strcpy(sql, "SELECT * FROM osm_node_tags LIMIT 10");
if(!(sqlite3_get_table(db_handle, sql, &results, &rows, &columns, &err_msg)))
{
    std::cerr << "Error: " << sqlite3_errmsg(db_handle);
    sqlite3_close(db_handle);
    return -1;
}
else
{
    std::cerr << "number of rows = " << rows;

    for (int i=1; i <= rows; i++)
    {  std::cerr << results[(i * columns) + 0] << std::endl;  }
}


return 1;
}

【问题讨论】:

    标签: c api sqlite


    【解决方案1】:

    我什至看不到任何不那么明显的错误,所以请注意sqlite3_get_table says

    这是为向后兼容而保留的旧接口。不推荐使用此接口。

    但它确实不应该被破坏。您是否仔细检查过该表实际上包含多于零行?

    【讨论】:

      【解决方案2】:

      http://ray.bsdart.org/man/sqlite/c3ref/open.html

      sqlite3_open_v2 成功时返回 SQLITE_OK(为零)。我认为你的条件是颠倒的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多