在 C/C++ 中使用 SQLite 通常需要包含 SQLite 的头文件,并链接 SQLite 的库文件。以下是一个简单的示例,演示如何在 C/C++ 中使用 SQLite 进行数据库操作:

步骤:

1. 下载 SQLite: 首先,你需要下载 SQLite 的预编译二进制文件或源代码。你可以从 [SQLite 官方网站](https://www.sqlite.org/download.html) 获取。

2. 包含头文件: 在你的 C/C++ 代码中包含 SQLite 的头文件。
    #include <sqlite3.h>

3. 连接 SQLite 库: 在编译时链接 SQLite 库文件。

    - 如果使用预编译二进制文件,你可能只需要链接 sqlite3 库。
    - 如果使用源代码,需要编译 SQLite 并链接生成的库文件。

4. 打开/创建数据库: 使用 sqlite3_open 函数打开现有数据库或创建新数据库。
    sqlite3* db;
    int rc = sqlite3_open("example.db", &db);

5. 执行 SQL 语句: 使用 sqlite3_exec 函数执行 SQL 语句。
    const char* sql = "CREATE TABLE IF NOT EXISTS employees (id INTEGER PRIMARY KEY, name TEXT, salary REAL);";
    rc = sqlite3_exec(db, sql, 0, 0, 0);

6. 插入数据: 使用参数化查询插入数据。
    const char* insert_sql = "INSERT INTO employees (name, salary) VALUES (?, ?);";
    sqlite3_stmt* stmt;
    rc = sqlite3_prepare_v2(db, insert_sql, -1, &stmt, 0);
    rc = sqlite3_bind_text(stmt, 1, "John Doe", -1, SQLITE_STATIC);
    rc = sqlite3_bind_double(stmt, 2, 50000.0);
    rc = sqlite3_step(stmt);
    rc = sqlite3_finalize(stmt);

7. 查询数据: 使用 sqlite3_prepare_v2 函数准备查询语句,使用 sqlite3_step 函数执行查询,然后使用 sqlite3_column_* 函数获取结果。
    const char* select_sql = "SELECT id, name, salary FROM employees;";
    rc = sqlite3_prepare_v2(db, select_sql, -1, &stmt, 0);
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        int id = sqlite3_column_int(stmt, 0);
        const char* name = (const char*)sqlite3_column_text(stmt, 1);
        double salary = sqlite3_column_double(stmt, 2);
        // 处理查询结果...
    }
    rc = sqlite3_finalize(stmt);

8. 关闭数据库: 使用 sqlite3_close 函数关闭数据库连接。
    sqlite3_close(db);

示例:
#include <sqlite3.h>
#include <iostream>

int main() {
    sqlite3* db;
    int rc = sqlite3_open("example.db", &db);

    if (rc != SQLITE_OK) {
        std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
        return rc;
    }

    const char* create_table_sql = "CREATE TABLE IF NOT EXISTS employees (id INTEGER PRIMARY KEY, name TEXT, salary REAL);";
    rc = sqlite3_exec(db, create_table_sql, 0, 0, 0);

    const char* insert_sql = "INSERT INTO employees (name, salary) VALUES (?, ?);";
    sqlite3_stmt* stmt;
    rc = sqlite3_prepare_v2(db, insert_sql, -1, &stmt, 0);
    rc = sqlite3_bind_text(stmt, 1, "John Doe", -1, SQLITE_STATIC);
    rc = sqlite3_bind_double(stmt, 2, 50000.0);
    rc = sqlite3_step(stmt);
    rc = sqlite3_finalize(stmt);

    const char* select_sql = "SELECT id, name, salary FROM employees;";
    rc = sqlite3_prepare_v2(db, select_sql, -1, &stmt, 0);
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        int id = sqlite3_column_int(stmt, 0);
        const char* name = (const char*)sqlite3_column_text(stmt, 1);
        double salary = sqlite3_column_double(stmt, 2);
        std::cout << "ID: " << id << ", Name: " << name << ", Salary: " << salary << std::endl;
    }
    rc = sqlite3_finalize(stmt);

    sqlite3_close(db);

    return 0;
}

请确保在实际项目中处理错误和异常情况,上述代码只是一个简单的示例。在实际应用中,还需要考虑数据库事务、错误处理、连接池等更复杂的情况。


转载请注明出处:http://www.zyzy.cn/article/detail/14203/SQLite