weixin_41225407 2024-10-16 12:23 采纳率: 0%
浏览 7
问题最晚将于10月24日00:00点结题

c++2013读写oracle

请写出相关例程:
注:
1.数据库连接要求使用SERVICE_NAME方式,进行连接
2.数据库连接信息需要配置文件来维护,不能写在程序中

若数据库连接信息不为配置文件来维护,须向客户进行说明且保证程序中是以SERVICE_NAME方式


```c++
以SERVICE_NAME的方式
jdbc:oracle:thin:@//<host>:<port>/<SERVICE_NAME>
或jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.18.23)(PORT=1521)))
  (CONNECT_DATA=(SERVICE_NAME=hmes)))

不要以SID的方式
jdbc:oracle:thin:@<host>:<port>:<SID> 
或:jdbc:oracle:thin:@<host>:<port>/<SID>

```

  • 写回答

13条回答 默认 最新

  • 阿里嘎多学长 2024-10-16 12:23
    关注

    AIGC生成:


    C++2013 读写 Oracle

    根据您的需求,我将提供一个使用 C++2013 读写 Oracle 的示例代码。

    首先,我们需要安装 Oracle 的 ODP.NET driver,并将其添加到项目中。

    接下来,我们可以使用 oci 库来连接 Oracle 数据库。下面是一个简单的示例代码:

    #include <oci.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    // 连接字符串
    const string CONNECT_STRING = "SERVICE_NAME=your_service_name";
    
    // 用户名和密码
    const string USERNAME = "your_username";
    const string PASSWORD = "your_password";
    
    // 配置文件路径
    const string CONFIG_FILE = "config.ini";
    
    // 读取配置文件
    void readConfig(const string& filename, string& connect_string, string& username, string& password) {
        ifstream file(filename);
        if (!file) {
            cout << "Failed to open config file!" << endl;
            return;
        }
    
        string line;
        while (getline(file, line)) {
            if (line.find("SERVICE_NAME") != string::npos) {
                connect_string = line.substr(line.find("=") + 1);
            } else if (line.find("USERNAME") != string::npos) {
                username = line.substr(line.find("=") + 1);
            } else if (line.find("PASSWORD") != string::npos) {
                password = line.substr(line.find("=") + 1);
            }
        }
        file.close();
    }
    
    // 连接 Oracle 数据库
    void connectOracle() {
        OCIEnv *envhp;
        OCIServer *srvhp;
        OCSSession *sesshp;
        OCIError *errhp;
        OCILogon *logonhp;
    
        // 创建环境句柄
        OCIEnvCreate((void **)&envhp, OCI_DEFAULT);
    
        // 创建服务器句柄
        OCIServerCreate(envhp, (text *)"your_database_name", (text *)CONNECT_STRING.c_str(), (ub2 *)0, (ub2 *)0, (ub4 *)0, (ub4 *)0, (void **)&srvhp, (OCIError **)0);
    
        // 创建会话句柄
        OCSSessionCreate(envhp, srvhp, (text *)"your_username", (text *)USERNAME.c_str(), (text *)PASSWORD.c_str(), (ub2 *)0, (ub2 *)0, (ub4 *)0, (ub4 *)0, (void **)&sesshp, (OCIError **)0);
    
        // 登录
        OCILogon(envhp, (void **)&logonhp, sesshp, (text *)"your_username", (text *)USERNAME.c_str(), (text *)PASSWORD.c_str(), (ub2 *)0, (ub2 *)0, (ub4 *)0, (ub4 *)0);
    
        // 释放资源
        OCILogoff(logonhp);
        OCSSessionRelease(sesshp);
        OCIServerRelease(srvhp);
        OCIEnvRelease(envhp);
    }
    
    // 读取 Oracle 数据库
    void readOracle() {
        // 连接 Oracle 数据库
        connectOracle();
    
        // 读取数据
        OCISql *sqlhp;
        OCILob *lobhp;
        OCILobLocator *lobloc;
    
        // 创建 SQL 句柄
        OCISqlCreate(envhp, (text *)"SELECT * FROM your_table_name", (ub2 *)0, (ub4 *)0, (void **)&sqlhp, (OCIError **)0);
    
        // 执行 SQL 语句
        OCISqlExecute(sqlhp);
    
        // 读取数据
        OCILobCreate(envhp, (text *)"your_column_name", (ub2 *)0, (ub4 *)0, (void **)&lobhp, (OCIError **)0);
        OCILobLocatorCreate(envhp, (text *)"your_column_name", (ub2 *)0, (ub4 *)0, (void **)&lobloc, (OCIError **)0);
    
        // 释放资源
        OCILobRelease(lobhp);
        OCILobLocatorRelease(lobloc);
        OCISqlRelease(sqlhp);
    }
    
    // 写入 Oracle 数据库
    void writeOracle() {
        // 连接 Oracle 数据库
        connectOracle();
    
        // 写入数据
        OCISql *sqlhp;
        OCILob *lobhp;
        OCILobLocator *lobloc;
    
        // 创建 SQL 句柄
        OCISqlCreate(envhp, (text *)"INSERT INTO your_table_name (your_column_name) VALUES (:your_column_name)", (ub2 *)0, (ub4 *)0, (void **)&sqlhp, (OCIError **)0);
    
        // 设置参数
        OCISqlBindByName(sqlhp, (text *)"your_column_name", (ub2 *)0, (ub4 *)0, (void **)&lobhp, (OCIError **)0);
        OCILobLocatorCreate(envhp, (text *)"your_column_name", (ub2 *)0, (ub4 *)0, (void **)&lobloc, (OCIError **)0);
    
        // 写入数据
        OCILobWrite(lobhp, (ub2 *)0, (ub4 *)0, (void **)&lobloc, (OCIError **)0);
    
        // 释放资源
        OCILobRelease(lobhp);
        OCILobLocatorRelease(lobloc);
        OCISqlRelease(sqlhp);
    }
    
    int main() {
        // 读取配置文件
        string connect_string, username, password;
        readConfig(CONFIG_FILE, connect_string, username, password);
    
        // 连接 Oracle 数据库
        connectOracle();
    
        // 读取数据
        readOracle();
    
        // 写入数据
        writeOracle();
    
        return 0;
    }
    

    这个示例代码使用 oci 库连接 Oracle 数据库,并使用 OCISqlOCILob 库来执行 SQL 语句和读写数据。

    请注意,这只是一个简单的示例代码,您需要根据您的实际需求进行修改和调整。

    在使用这个示例代码时,请确保您已经安装了 Oracle 的 ODP.NET driver,并将其添加到项目中。

    同时,也请注意这个示例代码中使用的连接字符串、用户名和密码都是假设的,您需要将它们替换为您的实际连接信息。

    如果您有任何问题或需要更多帮助,请随时问我。


    评论

报告相同问题?

问题事件

  • 创建了问题 今天

悬赏问题

  • ¥15 lvgl v8.2定时器提前到来
  • ¥20 关于#stm32#的问题:想用3*5的矩阵键盘输出数组来控制音乐模块播放15首音乐
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错
  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)