我对使用数据库有点陌生。我一直在学习 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