I'm attempting to connect to a MySQL database with the following code. But it throws EXC_BAD_ACCESS as soon as it gets to the "Query query = conn.query()" line. What could be the reason? Thanks in advance!

#include <iostream>
#include <mysql++/mysql++.h>

using namespace mysqlpp;

int main(int argc, const char * argv[])
{

    try {
        Connection conn(false);
        if (conn.connect("db", "localhost", "root", "root")) {
            Query query = conn.query("select * from django_site");
        }
    }
    catch (BadQuery er) {
        std::cerr << "Error: " << er.what();
        return -1;
    } catch (const BadConversion& er) {
        // Handle bad conversions
        std::cerr << "Conversion error: " << er.what() << std::endl <<
        "\tretrieved data size: " << er.retrieved <<
        ", actual size: " << er.actual_size << std::endl;
        return -1;
    } catch (const Exception& er) {
        // Catch-all for any other MySQL++ exceptions
        std::cerr << "Error: " << er.what() << std::endl;
        return -1;
    }

    return 0;
}

Printing the conn object returns

(mysqlpp::Connection) conn = {
  mysqlpp::OptionalExceptions = {
    exceptions_ = false
  }
  error_message_ = "\xe5$}\xff\x7f"
  driver_ = 0x00007fff5fc005a8
  co

What's more troubling is that the "conn_" variable is pointing to NULL

(mysqlpp::Query) query = {
  std::__1::ostream = {
    std::__1::basic_ios<char, std::__1::char_traits<char> > = {
      std::__1::ios_base = {
        __fmtflags_ = 0
        __precision_ = 0
        __width_ = 0
        __rdstate_ = 0
        __exceptions_ = 0
        __rdbuf_ = 0x0000000000000000
        __loc_ = 0x0000000000000000
        __fn_ = 0x0000000000000000
        __index_ = 0x0000000000000000
        __event_size_ = 0
        __event_cap_ = 0
        __iarray_ = 0x0000000000000000
        __iarray_size_ = 0
        __iarray_cap_ = 0
        __parray_ = 0x0000000000000000
        __parray_size_ = 0
        __parray_cap_ = 0
      }
      __tie_ = 0x0000000000000000
      __fill_ = 0
    }
  }
  mysqlpp::OptionalExceptions = {
    exceptions_ = false
  }
  template_defaults = {
    std::__1::vector<mysqlpp::SQLTypeAdapter, std::__1::allocator<mysqlpp::SQLTypeAdapter> > = size=0 {}
    parent_ = 0x0000000000000000
    processing_ = false
  }
  conn_ = 0x0000000000000000
  copacetic_ = false
  parse_elems_ = size=0 {}
  parsed_names_ = size=0 {}
  parsed_nums_ = size=0 {}
  sbuffer_ = {
    std::__1::basic_streambuf<char, std::__1::char_traits<char> > = {
      __loc_ = {
        __locale_ = 0x0000000000000000
      }
      __binp_ = 0x0000000000000000
      __ninp_ = 0x0000000000000000
      __einp_ = 0x0000000000000000
      __bout_ = 0x0000000000000000
      __nout_ = 0x0000000000000000
      __eout_ = 0x0000000000000000
    }
    __str_ = ""
    __hm_ = 0x0000000000000000
    __mode_ = 0
  }
}
有帮助吗?

解决方案 2

I should've posted back sooner. For others who ran into the same issue, the reason for the error is quite silly on my end. Turns out that I'm using C++11's std library, but MySQL++ wasn't originally compiled against it. Recompiling MySQL++ with the newer std lib fixes the problem.

其他提示

You are not checking the return type of the call to connect() before you create a query. Calling

Query query = conn.query();

if connect() fails will likely be the cause of this exception.

According to the manual pages http://tangentsoft.net/mysql++/doc/html/refman/classmysqlpp_1_1Query.html passing a NULL char* to the Connection::query() function is valid as this is the default argument so I would assume that your database connection request has failed. I would check the return type of

conn.connect("db", "localhost", "root", "root");

and double check your database name, user name and password strings that you are passing to this function.

Also, check that your application has the required permissions to be connecting to the database as root.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top