Home > Backend Development > C++ > body text

How does the C++ function library perform database management?

WBOY
Release: 2024-04-18 14:15:02
Original
340 people have browsed it

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++ 函数库如何进行数据库管理?

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

Transaction processing

sqlite3_exec(db, "BEGIN TRANSACTION");
// 执行多条查询
sqlite3_exec(db, "COMMIT");
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!