关于使用 Connector/C++ connect 时,出现std::bad_alloc错误的异常

qifa 2018-01-20 04:58:49
Connector/C++使用的是最新的32位的1.1.9版本
测试程序就是文档里面摘取的 Complete Example 2

int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;

try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;

/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "123456"); //这里出现std::bad_alloc错误的异常
/* Connect to the MySQL test database */
con->setSchema("test");

stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;

/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;

/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();

/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;

delete pstmt;
delete con;

}
catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "
<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() <<
" )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}



前段时间运行得还很正常的,不知道和系统有没有什么关系,有谁遇到这样的情况,怎么解决的
...全文
1436 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qifa 2018-02-06
  • 打赏
  • 举报
回复
引用 1 楼 bandaoyu 的回复:
你这是不是没连接成功就使用连接了?
不是这个问题, 官方下载页面有个警告, 好像是如果开发者使用的开发工具版本和官方编译Connector/C++版本不一致,可能会导致兼容性问题, 我重新编译Connector/C++源码, 用本地编译的版本就没这个问题了
bandaoyu 2018-02-05
  • 打赏
  • 举报
回复
你这是不是没连接成功就使用连接了?

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧