我对使用数据库有点陌生。我一直在学习oracle sql,并从他们的网站安装了oracle express edition 18c。我通常通过提供的SQL*Plus工具执行sql查询。现在,我真的希望能够从项目的c++程序连接到数据库。我听说了odbc,并从他们的网站上下载了我的oracle版本的odbc驱动程序和即时客户端。然后我找到了一个名为SQLAPI++的第三方库,它可以用来通过c++连接到数据库。我下载了这个库,并将其包含在我的项目中。我在windows 10上使用代码块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发布于 2020-09-14 06:52:33
我知道我在这方面完全是新手,但不管怎样,我还是会把对我有用的东西贴出来。我最终没能使用odbc连接到我的oracle 18c数据库,但occi做到了。我使用SA_Oracle_Client作为第四个参数,使用"host_name:port_no/service_name“作为连接字符串(第一个参数)。我刚刚从tnsnames.ora文件中复制了这些值。在此之前,我必须将环境变量TNS_ADMIN设置为指向oracle_home安装文件夹中的tnsnames.ora目录,并安装32位即时客户端,因为我的编译器是32位的,但我的系统、oracle安装和我之前下载的即时客户端都是64位的。感谢所有帮助我们的人:)
发布于 2020-09-12 20:32:40
我认为您可能遗漏了特定于oracle用例的参考资料。
#include "oraAPI.h"
IsaAPI *pApi = con.NativeAPI();
oraAPI *pNativeAPI = (oraAPI *)pApi;https://stackoverflow.com/questions/63860271
复制相似问题