C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用

C++ app MySQL odbc database connection error: terminate called after throwing an instance of 'otl_tmpl_exception<>

本文关键字:exception tmpl lt gt 调用 终止 实例 otl 数据库连接 odbc MySQL      更新时间:2023-10-16

我目前正在调试一个容器化C++应用程序,似乎它抛出异常并抱怨数据库连接,错误:

terminate called after throwing an instance of 'otl_tmpl_exception<odbc::otl_exc, odbc::otl_conn, odbc::otl_cur>'
Aborted

main(( 中的代码如下:

int main(int ac, char *av[])
{
auto otl_connect = std::make_unique<odbc::otl_connect>("Driver={/usr/local/lib/libmyodbc8a.so};server=xxx.x.x.x;port=xxxx;database=xxxx;user=xxx;password=xxx");
std::stringstream query;
query << "SELECT x FROM xxx.xxxs;";
odbc::otl_stream the_stream(1000, query.str().c_str(), *otl_connect);
std::string
int val;
while(!the_stream.eof())
{
the_stream >> xxx >> val;
std::cout << xxx << " " << val << "n";
}
the_stream.close();
}

我对C++完全陌生,有人可以解释一下main((中的代码在做什么以及如何修复异常错误消息,我已经为此工作了一整个下午,筋疲力尽......帮助!!!!

我对 Oracle、ODBC 和 DB2-CLI 模板库不是很熟悉,但我在 Ubuntu Linux 上的 MySql 数据库中使用它。

这就是我能够运行简单查询的方式。我认为下面的代码是不言自明的。

如您所见,它与您的代码完全不同。驱动程序是mysql.您必须将...替换为数据库的实际数据库名称、用户名和密码。您还必须首先初始化 ODBC 环境,然后使用rlogon()连接到数据库。

#include <iostream>
#define OTL_ODBC // Compile OTL 4.0/ODBC
#define OTL_ODBC_UNIX
#include "otlv4.h"
int main()
{
otl_connect db; // connect object
otl_connect::otl_initialize(); // initialize ODBC environment
try {
db.rlogon("DRIVER=mysql;DB=...;UID=...;PWD=..."); // connect to ODBC
otl_stream os(50, "SELECT id FROM task", db);
int id;
// SELECT automatically executes when all input variables are assigned
while (!os.eof())
{
os >> id;
std::cout << "id=" << id << std::endl;
}
}
catch(otl_exception& p) {       // intercept OTL exceptions
std::cerr << p.msg << std::endl;      // print out error message
std::cerr << p.stm_text << std::endl; // print out SQL that caused the error
std::cerr << p.sqlstate << std::endl; // print out SQLSTATE message
std::cerr << p.var_info << std::endl; // print out the variable that caused the error
}
return 0;
}

确保要从查询结果中读取的每个字段都有一个变量。似乎您无法将值提取到std::string变量中;您必须使用 char 数组(例如char name[20](改为。

希望这有帮助。