gpt4 book ai didi

c - 在 C 中同步查询 SQLite 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:09 25 4
gpt4 key购买 nike

我正在尝试在 C 中使用 SQLite。

我知道的接口(interface)是用来查询的,

sqlite3_exec

仅允许异步查询数据(就我在搜索中发现的而言)。

有没有什么方法可以同步从 SQLite 数据库中获取“Select count(*) ...”的结果,而不必实现等待数据并将其返回到相同函数的东西?

我实际上想实现一个“howManyItems”方法,将实际结果返回给调用者...

谢谢。

最佳答案

假设一个打开的sqlite3 *db,使用sqlite3_prepare_v2()sqlite3_step() (为简洁起见忽略错误检查):

sqlite3_stmt *statement = null;

// prepare our query
sqlite3_prepare_v2(db, "select count(*) from foo;", -1, &statement, 0);

// if there were parameters to bind, we'd do that here

// retrieve the first row (only row, in this case) of the results
int result = sqlite3_step(statement);

if (result == SQLITE_ROW)
{
// retrieve the value of the first column (0-based)
int count = sqlite3_column_int(statement, 0);

// do something with count
}

// free our statement
sqlite3_finalize(statement);

关于c - 在 C 中同步查询 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25941088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com