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

自作多情 提交于 2020-07-23 08:21:27

问题


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

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();
}

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!!!!


回答1:


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.

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;
}

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.

Hope this helps.



来源:https://stackoverflow.com/questions/62717701/c-app-mysql-odbc-database-connection-error-terminate-called-after-throwing-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!