为什么get_driver_instance()抛出std :: bad_alloc?

时间:2011-12-09 08:24:19

标签: c++ mysql driver

我正在尝试从mysql.com网站运行一个C ++ MySQL示例。

#define DBHOST "tcp://127.0.0.1:3306/"
#define USER "root"
#define PASSWORD ""

//...

string url(DBHOST);
const string user(USER);
const string password(PASSWORD);
const string database(DATABASE);

try {
    driver = sql::mysql::get_driver_instance();

    /* create a database connection using the Driver */

    con = driver->connect(url, user, password);

我明白了:

First-chance exception at 0x75c99673 in DBTest.exe: Microsoft C++ exception:      std::bad_alloc at memory location 0x0027f1ec..
First-chance exception at 0x75c99673 in DBTest.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0027eb78..
First-chance exception at 0x75c99673 in DBTest.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x75c99673 in DBTest.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0027eb78..
The program '[1236] DBTest.exe: Native' has exited with code -529697949 (0xe06d7363).

...在driver->connect(...)。我试过没有将它分配给con,同样的事情(意思是get_driver_instance()中的问题)?

1 个答案:

答案 0 :(得分:-1)

有点晚了但是为了解决这个问题,我尝试了这个,它对我有用

sql::mysql::Driver *driver = get_driver_instance();

const sql::SQLString host = "tcp://127.0.0.1:3306";
const sql::SQLString user = "root";
const sql::SQLString pass = "*****";

sql::mysql::Connection *con = driver->connect(host, user, pass);