Home >Backend Development >C++ >How to handle database connections and operations using C++?

How to handle database connections and operations using C++?

WBOY
WBOYOriginal
2024-06-01 19:24:01985browse

在C++中使用Data Access Objects (DAO) 库连接和操作数据库,包括建立数据库连接、执行SQL查询、插入新记录和更新现有记录。具体步骤为:1. 包含必要的库语句;2. 打开数据库文件;3. 创建Recordset对象执行SQL查询或操作数据;4. 遍历结果或按照具体需求更新记录。

How to handle database connections and operations using C++?

如何在C++中进行数据库连接和操作

C++能够使用不同的库来连接和操作数据库,最常用的库是[Data Access Objects (DAO) Library](https://docs.microsoft.com/en-us/cpp/win32/data-access-objects-library?view=msvc-170)。

建立数据库连接

#include <dao.h>

int main()
{
    try
    {
        // 打开数据库文件
        Database db("mydb.mdb");

        // or
        Database db(L"c:\\mydb\\mydb.mdb", DB_SHARED_DENY);

        return 0;
    }
    catch (const DatabaseException& e)
    {
        std::cerr << "Error connecting to the database: " << e.what() << std::endl;
        return 1;
    }
}

执行SQL查询

#include <dao.h>

int main()
{
    try
    {
        // 打开数据库文件
        Database db("mydb.mdb");

        // 创建一个Recordset对象来执行SQL查询
        Recordset rs = db.OpenRecordset("SELECT * FROM Customers");

        // 遍历并打印结果
        while (!rs.IsEOF())
        {
            std::cout << "Customer ID: " << rs("CustomerID").lVal << std::endl;
            std::cout << "Customer Name: " << rs("CustomerName").StringValue << std::endl;
            std::cout << "Customer Email: " << rs("CustomerEmail").StringValue << std::endl;

            rs.MoveNext();
        }

        return 0;
    }
    catch (const DatabaseException& e)
    {
        std::cerr << "Error executing SQL query: " << e.what() << std::endl;
        return 1;
    }
}

插入新记录

#include <dao.h>

int main()
{
    try
    {
        // 打开数据库文件
        Database db("mydb.mdb");

        // 创建一个Recordset对象来插入新记录
        Recordset rs = db.OpenRecordset("Customers");

        rs.AddNew();
        rs("CustomerID") = 4;
        rs("CustomerName") = "New Customer";
        rs("CustomerEmail") = "new-customer@email.com";

        // 更新记录
        rs.Update();

        return 0;
    }
    catch (const DatabaseException& e)
    {
        std::cerr << "Error inserting new record: " << e.what() << std::endl;
        return 1;
    }
}

更新现有记录

#include <dao.h>

int main()
{
    try
    {
        // 打开数据库文件
        Database db("mydb.mdb");

        // 创建一个Recordset对象来执行SQL查询
        Recordset rs = db.OpenRecordset("SELECT * FROM Customers WHERE CustomerID = 4");

        // 更新记录
        if (!rs.IsEOF())
        {
            rs.Edit();
            rs("CustomerEmail") = "updated-new-customer@email.com";
            rs.Update();
        }

        return 0;
    }
    catch (const DatabaseException& e)
    {
        std::cerr << "Error updating existing record: " << e.what() << std::endl;
        return 1;
    }
}

The above is the detailed content of How to handle database connections and operations using C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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