C ++应用程序MySQL odbc数据库连接错误:抛出'otl_tmpl_exception<>实例后终止调用 [英] C++ app MySQL odbc database connection error: terminate called after throwing an instance of 'otl_tmpl_exception<>

查看:421
本文介绍了C ++应用程序MySQL odbc数据库连接错误:抛出'otl_tmpl_exception<>实例后终止调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently debugging a containerized C++ application, it seems like it's throwing exception and complaining about the database connection, error:

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

main()中的代码如下:

The code in main() is below:

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()中的代码在做什么以及如何解决异常错误消息,我整个下午都在工作,筋疲力尽.... help !!!!

I'm totally new to C++, can someone explain what the codes in main() is doing and how to fix the exception error message, I've been working on this for a whole afternoon, exhausted....help!!!!

推荐答案

我对Oracle,ODBC和DB2-CLI模板库不是很熟悉,但是我可以在Ubuntu Linux上将其与MySql数据库一起使用。

I'm not very familiar with the Oracle, ODBC and DB2-CLI Template Library but I had a go using it with a MySql database on my Ubuntu Linux.

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

This is how I was able to run a simple query. I think the code below is fairly self-explanatory.

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

As you'll see, it's quite different from your code. The driver is mysql. You have to substitute ... with the real database name, user name and password of your database. You also have to initialise the ODBC environment first and connect to your database using 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] )。

Make sure that you have a variable for each field you want to read from the query result. It seems that you can't extract values into std::string variables; you have to use char arrays (e.g. char name[20]) instead.

希望这会有所帮助。

这篇关于C ++应用程序MySQL odbc数据库连接错误:抛出'otl_tmpl_exception&lt;&gt;实例后终止调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆