0

我对使用数据库有点陌生。我一直在学习 oracle sql 并从他们的站点安装了 oracle express edition 18c。我一般通过提供的 SQL*Plus 工具执行 sql 查询。现在,我真的希望能够从我的 C++ 程序连接到项目的数据库。我听说了 odbc,我去他们的网站下载了我的 oracle 版本的 odbc 驱动程序和即时客户端。然后我找到了一个名为 SQLAPI++ 的第三方库,它可用于使用 c++ 连接到数据库。我下载了该库并将其包含在我的项目中。我在 Windows 10 上使用 codeblocks IDE。我试图运行这个程序来测试我是否可以连接到数据库-

#include<iostream>
#include<SQLAPI.h>

using namespace std;

int main()
{
    SAConnection conn;
    try
    {
        conn.Connect("Data Source=LIBRARY;User Id=my_uid;Password=my_pass;Integrated Security=no","my_uid","my_pass",SA_Oracle_Client);
//LIBRARY is my dsn that i created by using the odbc 64-bit admin. tool in the user dsn tab. I used the "Oracle in instantclient_18_5" driver for it.
        if(conn.isConnected()==TRUE)
        {
            cout<<"Connected successfully"<<endl;
            conn.Disconnect();
            cout<<"Disconnected successfully"<<endl;
        }
        else
            cout<<"Failed to connect"<<endl;
    }
    catch(SAException &a)
    {

        cout<<endl<<a.ErrText().GetMultiByteChars()<<endl;
    }
}

没有编译器错误或警告。现在它打印——“ORA-12154:TNS:无法解析指定的连接标识符”。任何帮助将不胜感激!>.<

编辑:现在我运行调试器,这就是我所看到的 -

Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.9.1
Child process PID: 15224
In __cxa_throw () ()
1094    oraAPI.cpp: No such file or directory.
#1  0x00494eb2 in oraAPI::Check (this=0x1307fe8, sCommandText=..., status=-1, hndlp=0x95f208, type=2, pOCIStmt=0x0) at oraAPI.cpp:1094
In __cxa_get_globals () ()
#3  0x00494c06 in oraAPI::Check (this=0x1307fe8, status=-1, hndlp=0x95f208, type=2, pOCIStmt=0x0) at oraAPI.cpp:1018
1018    in oraAPI.cpp
Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c
At ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c:126
Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c
At ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c:128
In __cxa_get_globals () ()
1730    SQLAPI.cpp: No such file or directory.
#7  0x004054bb in SAConnection::NativeAPI (this=0x5710b2 <__DTOR_LIST__+306>) at SQLAPI.cpp:1730
In __cxa_throw () ()
1018    oraAPI.cpp: No such file or directory.
#2  0x00494c06 in oraAPI::Check (this=0x1307fe8, status=-1, hndlp=0x95f208, type=2, pOCIStmt=0x0) at oraAPI.cpp:1018
[Inferior 1 (process 15224) exited normally]
Debugger finished with status 0

4

2 回答 2

0

我认为您可能缺少特定于 oracle 用例的参考

#include "oraAPI.h"
IsaAPI *pApi = con.NativeAPI();
oraAPI *pNativeAPI = (oraAPI *)pApi;
于 2020-09-12T12:32:40.220 回答
0

我知道我在这方面完全是菜鸟,但无论如何我都会发布对我有用的东西。我最终没有设法使用 odbc 连接到我的 oracle 18c 数据库,但 occi 成功了。我使用 SA_Oracle_Client 作为第四个参数,使用“host_name:port_no/service_name”作为连接字符串(第一个参数)。我刚刚从 tnsnames.ora 文件中复制了这些值。在此之前,我必须设置环境变量 TNS_ADMIN 指向 oracle_home 安装文件夹中的 tnsnames.ora 目录并安装 32 位 Instantclient,因为我的编译器是 32 位但我的系统,oracle 安装和以前的 Instantclient我下载的都是64位的。感谢所有帮助过的人:)

于 2020-09-13T22:52:33.477 回答