此查询以列属性为整型为依据,也可根据字符串类型进行相应的函数接口修改
#include <stdio.h>
#include <sqlite3.h>int main() {sqlite3 *db;int rc;const char *db_path = "test.db"; // 数据库文件路径// 打开数据库rc = sqlite3_open(db_path, &db);if (rc != SQLITE_OK) {fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));return 1;}// 准备查询const char *sql = "SELECT age FROM table1"; // SQL查询语句 表名字、列名称需要更换为具体操作的sqlite3_stmt *stmt;rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);if (rc != SQLITE_OK) {fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return 1;}// 执行查询while (sqlite3_step(stmt) == SQLITE_ROW) {int age = sqlite3_column_int(stmt, 0);if (age) {printf("age: %d\n", age);}}// 最终检查if (sqlite3_finalize(stmt) != SQLITE_OK) {fprintf(stderr, "SQL finalize error: %s\n", sqlite3_errmsg(db));}// 关闭数据库sqlite3_close(db);return 0;
}
若为字符串,改写成sqlite3_column_text