Sqlite C 接口获取单值结果 [英] Sqlite C interface get single value result

查看:36
本文介绍了Sqlite C 接口获取单值结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入 Sqlite 时

When I type into Sqlite

SELECT Count(*) FROM tabl;

它返回一个数字.

准备语句后如何使用C接口获取该编号?

How do I use the C interface to obtain this number after preparing a statement?

推荐答案

打开数据库文件,准备语句,做步骤(即如果结果多于一行,就得一一取),提取列值,完成语句,关闭数据库.

Open the database file, prepare the statement, make steps (i.e., if you have more than one row in results you have to fetch them one by one), extract column values, finalize the statement, close the database.

像这样:

sqlite3_stmt* stmt = NULL;

sqlite3* local_db = NULL;

sqlite3_open("filename.sqlite", &local_db);

int retval, idx;
char sql[2048];

sprintf(sql, "select Something from Somewhere;");

// execute statement
retval = sqlite3_prepare_v2(local_db, sql, -1, &stmt, 0);

if(retval)
{
    printf("Selecting data from DB Failed (err_code=%d)\n", retval);
    return;
}

// iterate rows
idx = 0;

// for multiple results
while(1)
{
    // fetch a row's status
    retval = sqlite3_step(stmt);

    if(retval == SQLITE_ROW)
    {
        Something =
             (int)sqlite3_column_int(stmt, 0);
             // or other type - sqlite3_column_text etc.
        // ... fetch other columns, if there are any
    }
    else if(retval == SQLITE_DONE)
    {
        break;
    }
    else
    {
        sqlite3_finalize(stmt);
        printf("Some error encountered\n");
        break;
    }
}

sqlite3_finalize(stmt);

sqlite3_close(local_db);

使用此代码,查找所有 API 调用(open、prepare_v2、step、column、finalize).

Use this code, look for all the API calls (open, prepare_v2, step, column, finalize).

如果这很难,那么首先你应该熟悉 C 本身.

If this is hard, then at first you should become familiar with C itself.

这篇关于Sqlite C 接口获取单值结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆