SQLite – C/C++
                           
天天向上
发布: 2025-03-05 22:56:01

原创
180 人浏览过

SQLite 提供了一个 C 语言接口,允许开发者使用 C/C++ 直接操作 SQLite 数据库。你可以使用 SQLite 的 C API 来创建、查询和管理数据库。

SQLite C API 介绍

SQLite 主要通过 sqlite3.h 头文件提供 API,常用函数包括:

  • sqlite3_open():打开数据库
  • sqlite3_close():关闭数据库
  • sqlite3_exec():执行 SQL 语句
  • sqlite3_prepare_v2()sqlite3_step()sqlite3_finalize():处理 SQL 查询
  • sqlite3_errmsg():获取错误信息

SQLite C 示例

以下是一个简单的 C 语言示例,展示如何创建 SQLite 数据库并执行 SQL 语句。

1. 安装 SQLite C 库

如果你还没有 SQLite C 库,可以安装:

sudo apt install libsqlite3-dev  # Ubuntu/Debian
sudo yum install sqlite-devel     # CentOS
brew install sqlite               # macOS

2. C 语言示例:创建数据库并插入数据

#include <stdio.h>
#include <sqlite3.h>

int main() {
    sqlite3 *db;
    char *errMsg = 0;
    int rc;

    // 打开数据库(如果不存在则创建)
    rc = sqlite3_open("test.db", &db);
    if (rc) {
        printf("无法打开数据库: %s\n", sqlite3_errmsg(db));
        return 1;
    } else {
        printf("数据库打开成功!\n");
    }

    // 创建表
    const char *sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);"
                      "INSERT INTO users (name) VALUES ('Alice'), ('Bob');";

    rc = sqlite3_exec(db, sql, 0, 0, &errMsg);
    if (rc != SQLITE_OK) {
        printf("SQL 错误: %s\n", errMsg);
        sqlite3_free(errMsg);
    } else {
        printf("数据插入成功!\n");
    }

    // 关闭数据库
    sqlite3_close(db);
    return 0;
}

编译 & 运行

gcc -o sqlite_demo sqlite_demo.c -lsqlite3
./sqlite_demo

使用 SQLite 进行查询

如果你想要 查询数据,可以使用 sqlite3_prepare_v2()sqlite3_step()

#include <stdio.h>
#include <sqlite3.h>

int main() {
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;

    // 打开数据库
    rc = sqlite3_open("test.db", &db);
    if (rc) {
        printf("无法打开数据库: %s\n", sqlite3_errmsg(db));
        return 1;
    }

    // 预编译 SQL 语句
    const char *sql = "SELECT id, name FROM users;";
    rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);

    if (rc != SQLITE_OK) {
        printf("SQL 错误: %s\n", sqlite3_errmsg(db));
    } else {
        printf("查询结果:\n");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int id = sqlite3_column_int(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            printf("ID: %d, Name: %s\n", id, name);
        }
    }

    // 释放资源
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return 0;
}

编译 & 运行

gcc -o sqlite_query sqlite_query.c -lsqlite3
./sqlite_query

C++ 使用 SQLite

如果你在 C++ 中使用 SQLite,可以使用 SQLite 官方的 C API,或者使用 SQLite C++ 封装库(如 sqlite-modern-cpp)。

C++ 示例

#include <iostream>
#include <sqlite3.h>

int main() {
    sqlite3 *db;
    if (sqlite3_open("test.db", &db)) {
        std::cerr << "无法打开数据库: " << sqlite3_errmsg(db) << std::endl;
        return 1;
    }
    std::cout << "数据库打开成功!" << std::endl;

    sqlite3_close(db);
    return 0;
}

编译 & 运行

g++ -o sqlite_cpp sqlite_cpp.cpp -lsqlite3
./sqlite_cpp

总结

1、SQLite 提供 C API,可以直接在 C/C++ 程序中使用。

2、核心 API

    • sqlite3_open() / sqlite3_close()
    • sqlite3_exec() 执行 SQL 语句
    • sqlite3_prepare_v2() / sqlite3_step() / sqlite3_finalize() 处理查询
    • sqlite3_errmsg() 获取错误信息

    3、C++ 可以直接使用 SQLite C API,也可以使用更现代的 C++ 封装库(如 sqlite-modern-cpp)。

      如果你想要更深入学习 SQLite C API,推荐阅读官方文档:
      🔗 SQLite C API 官方文档

      更多详细内容请关注其他相关文章。

      发表回复 0

      Your email address will not be published. Required fields are marked *