C function library can be used for database management. It provides a series of functions through the <sqlite3.h> header file to support operations such as connection, table creation, data insertion, query, and transaction processing. The library is suitable for management and database interaction. common tasks.
C function library for database management
C The standard library provides a wide range of functions to handle common functions related to database interaction. Task. These function libraries mainly come from the <sqlite3.h>
header file.
Connect to the database
sqlite3 *db; int rc = sqlite3_open("database.db", &db);
Create table
char *zErrMsg = 0; int rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY, name TEXT)", NULL, 0, &zErrMsg);
Insert data
sqlite3_stmt *stmt; sqlite3_prepare_v2(db, "INSERT INTO mytable (name) VALUES (?)", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, "John Doe", -1, SQLITE_STATIC); sqlite3_step(stmt); sqlite3_finalize(stmt);
Query data
sqlite3_stmt *stmt; sqlite3_prepare_v2(db, "SELECT name FROM mytable WHERE id=?", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, 1); while (sqlite3_step(stmt) == SQLITE_ROW) { printf("%s\n", sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt);
Transaction processing
sqlite3_exec(db, "BEGIN TRANSACTION"); // 执行多条查询 sqlite3_exec(db, "COMMIT");
Practical case: managing student information database
#include <iostream> #include <sqlite3.h> using namespace std; int main() { sqlite3 *db; int rc = sqlite3_open("students.db", &db); if (rc) { cerr << "Error opening database: " << sqlite3_errmsg(db) << endl; return -1; } // 创建表 char *zErrMsg = 0; rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)", NULL, 0, &zErrMsg); if (rc) { cerr << "Error creating table: " << zErrMsg << endl; sqlite3_free(zErrMsg); sqlite3_close(db); return -1; } // 插入数据 sqlite3_stmt *stmt; rc = sqlite3_prepare_v2(db, "INSERT INTO students (name, age) VALUES (?, ?)", -1, &stmt, NULL); if (rc) { cerr << "Error preparing insert statement: " << sqlite3_errmsg(db) << endl; sqlite3_close(db); return -1; } // 插入多条数据 for (int i = 0; i < 5; i++) { sqlite3_bind_text(stmt, 1, "Student " + to_string(i), -1, SQLITE_STATIC); sqlite3_bind_int(stmt, 2, 20 + i); sqlite3_step(stmt); sqlite3_reset(stmt); } sqlite3_finalize(stmt); // 查询数据 stmt = nullptr; rc = sqlite3_prepare_v2(db, "SELECT * FROM students", -1, &stmt, NULL); if (rc) { cerr << "Error preparing select statement: " << sqlite3_errmsg(db) << endl; sqlite3_close(db); return -1; } while (sqlite3_step(stmt) == SQLITE_ROW) { int id = sqlite3_column_int(stmt, 0); const char *name = sqlite3_column_text(stmt, 1); int age = sqlite3_column_int(stmt, 2); cout << "Record " << id << ": Name = " << name << ", Age = " << age << endl; } sqlite3_finalize(stmt); sqlite3_close(db); return 0; }
By running this program, a table named "students" can be created in the database named "students.db", which contains three columns: id, name and age. It also inserts five pieces of test data and queries the database to retrieve student information.
The above is the detailed content of How does the C++ function library perform database management?. For more information, please follow other related articles on the PHP Chinese website!