SQLite 数据库 C/C++ 原生接口开发实战

SQLite 数据库阿木 发布于 4 天前 1 次阅读


C/C++ 原生接口开发实战:SQLite 数据库操作指南

SQLite 是一款轻量级的数据库引擎,以其小巧的体积、高效的性能和跨平台的特点,在嵌入式系统、移动应用和桌面应用程序中得到了广泛的应用。C/C++ 作为系统编程的利器,能够直接操作 SQLite 数据库,实现高效的数据库管理。本文将围绕 C/C++ 原生接口开发实战,详细介绍 SQLite 数据库的安装、配置、连接、查询、更新、删除等操作。

环境准备

在开始之前,请确保您的系统已安装以下软件:

1. SQLite:从官方网站下载并安装 SQLite。

2. C/C++ 编译器:如 GCC、Clang 或 Visual Studio。

3. 开发环境:如 Visual Studio、Code::Blocks 或其他支持 C/C++ 的 IDE。

SQLite 数据库简介

SQLite 是一个自包含、无服务器、零配置的数据库引擎。它使用预编译的 SQL 语句进行数据操作,支持标准的 SQL 语法。SQLite 数据库文件通常以 `.db` 为扩展名。

连接 SQLite 数据库

在 C/C++ 中,我们可以使用 SQLite 的 C API 来连接数据库。以下是一个简单的示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


} else {


fprintf(stderr, "成功打开数据库");


}

// 关闭数据库


sqlite3_close(db);


return 0;


}


在上面的代码中,我们首先包含了 `sqlite3.h` 头文件,然后创建了一个 `sqlite3` 类型的指针 `db`。使用 `sqlite3_open` 函数打开数据库文件 `example.db`,如果成功,则返回 0,否则返回错误码。使用 `sqlite3_close` 函数关闭数据库连接。

创建表

在 SQLite 中,我们可以使用 `CREATE TABLE` 语句创建表。以下是一个示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;


const char sql = "CREATE TABLE IF NOT EXISTS example ("


"id INTEGER PRIMARY KEY AUTOINCREMENT, "


"name TEXT NOT NULL, "


"age INTEGER);";

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


}

// 执行 SQL 语句


rc = sqlite3_exec(db, sql, 0, 0, 0);


if (rc != SQLITE_OK) {


fprintf(stderr, "SQL 错误: %s", sqlite3_errmsg(db));


} else {


fprintf(stderr, "表创建成功");


}

// 关闭数据库


sqlite3_close(db);


return 0;


}


在上面的代码中,我们首先定义了一个 SQL 语句 `sql`,然后使用 `sqlite3_exec` 函数执行该语句。如果执行成功,则返回 0,否则返回错误码。

插入数据

在 SQLite 中,我们可以使用 `INSERT INTO` 语句插入数据。以下是一个示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;


const char sql = "INSERT INTO example (name, age) VALUES ('张三', 20);";

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


}

// 执行 SQL 语句


rc = sqlite3_exec(db, sql, 0, 0, 0);


if (rc != SQLITE_OK) {


fprintf(stderr, "SQL 错误: %s", sqlite3_errmsg(db));


} else {


fprintf(stderr, "数据插入成功");


}

// 关闭数据库


sqlite3_close(db);


return 0;


}


在上面的代码中,我们定义了一个 SQL 语句 `sql`,然后使用 `sqlite3_exec` 函数执行该语句。如果执行成功,则返回 0,否则返回错误码。

查询数据

在 SQLite 中,我们可以使用 `SELECT` 语句查询数据。以下是一个示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;


sqlite3_stmt stmt;


const char sql = "SELECT id, name, age FROM example;";

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


}

// 预编译 SQL 语句


rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);


if (rc != SQLITE_OK) {


fprintf(stderr, "SQL 错误: %s", sqlite3_errmsg(db));


return 1;


}

// 遍历结果集


while (sqlite3_step(stmt) == SQLITE_ROW) {


int id = sqlite3_column_int(stmt, 0);


const char name = (const char )sqlite3_column_text(stmt, 1);


int age = sqlite3_column_int(stmt, 2);


printf("ID: %d, Name: %s, Age: %d", id, name, age);


}

// 清理资源


sqlite3_finalize(stmt);


sqlite3_close(db);


return 0;


}


在上面的代码中,我们首先使用 `sqlite3_prepare_v2` 函数预编译 SQL 语句,然后使用 `sqlite3_step` 函数遍历结果集。对于每一行数据,我们使用 `sqlite3_column_int` 和 `sqlite3_column_text` 函数获取列值。

更新数据

在 SQLite 中,我们可以使用 `UPDATE` 语句更新数据。以下是一个示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;


const char sql = "UPDATE example SET age = 21 WHERE name = '张三';";

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


}

// 执行 SQL 语句


rc = sqlite3_exec(db, sql, 0, 0, 0);


if (rc != SQLITE_OK) {


fprintf(stderr, "SQL 错误: %s", sqlite3_errmsg(db));


} else {


fprintf(stderr, "数据更新成功");


}

// 关闭数据库


sqlite3_close(db);


return 0;


}


在上面的代码中,我们定义了一个 SQL 语句 `sql`,然后使用 `sqlite3_exec` 函数执行该语句。如果执行成功,则返回 0,否则返回错误码。

删除数据

在 SQLite 中,我们可以使用 `DELETE` 语句删除数据。以下是一个示例:

c

include <sqlite3.h>

int main() {


sqlite3 db;


int rc;


const char sql = "DELETE FROM example WHERE name = '张三';";

// 打开数据库


rc = sqlite3_open("example.db", &db);


if (rc) {


fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));


return 1;


}

// 执行 SQL 语句


rc = sqlite3_exec(db, sql, 0, 0, 0);


if (rc != SQLITE_OK) {


fprintf(stderr, "SQL 错误: %s", sqlite3_errmsg(db));


} else {


fprintf(stderr, "数据删除成功");


}

// 关闭数据库


sqlite3_close(db);


return 0;


}


在上面的代码中,我们定义了一个 SQL 语句 `sql`,然后使用 `sqlite3_exec` 函数执行该语句。如果执行成功,则返回 0,否则返回错误码。

总结

本文介绍了 C/C++ 原生接口开发实战中 SQLite 数据库的基本操作,包括连接、创建表、插入、查询、更新和删除数据。通过学习这些操作,您可以轻松地使用 C/C++ 编程语言进行数据库编程。在实际应用中,您可以根据需要扩展这些操作,实现更复杂的数据库管理功能。