使用MySQL ++会抛出EXC_BAD_ACCESS

时间:2013-09-03 14:59:41

标签: c++ mysql mysql++

我正在尝试使用以下代码连接到MySQL数据库。但是一旦到达“Query query = conn.query()”行,它就会抛出EXC_BAD_ACCESS。可能是什么原因?提前谢谢!

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

打印conn对象返回

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

更令人不安的是,“conn_”变量指向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 个答案:

答案 0 :(得分:1)

在创建查询之前,您没有检查connect()调用的返回类型。调用

Query query = conn.query();

如果connect()失败,则可能是导致此异常的原因。

根据手册页http://tangentsoft.net/mysql++/doc/html/refman/classmysqlpp_1_1Query.html将NULL char*传递给Connection::query()函数是有效的,因为这是默认参数,因此我假设您的数据库连接请求失败。我会检查

的返回类型
conn.connect("db", "localhost", "root", "root");

并仔细检查您传递给此函数的数据库名称,用户名和密码字符串。

另外,请检查您的应用程序是否具有以root身份连接到数据库所需的权限。

答案 1 :(得分:0)

我应该早点回复。对于遇到同样问题的其他人来说,错误的原因在我看来是非常愚蠢的。事实证明我正在使用C ++ 11的std库,但MySQL ++最初并不是针对它编译的。使用较新的std lib重新编译MySQL ++可以解决问题。