1、使用c++返回mysql中的varchar类型数据,resultset的getString方法返回该数据报错,使用try catch语句显示为bad allocation.
2、代码:
#include <iostream>
#include <map>
#include <string>
#include <memory>
#include "mysql_driver.h"
#include "mysql_connection.h"
#include "cppconn/driver.h"
#include "cppconn/statement.h"
#include "cppconn/prepared_statement.h"
#include "cppconn/metadata.h"
#include "cppconn/exception.h"
#include <typeinfo>
using namespace std;
using namespace sql;
int main()
{
sql::mysql::MySQL_Driver *driver = 0;
sql::Connection *conn = 0;
try
{
driver = sql::mysql::get_mysql_driver_instance();
conn = driver->connect("tcp://localhost:3306/jwsystem", "root", "123456");
cout << "succeed!" << endl;
}
catch (...)
{
cout << "fail!" << endl;
}
sql::Statement* stat = conn->createStatement();
stat->execute("set names 'gbk'");
ResultSet *res;
res = stat->executeQuery("select * from semester");
while (res->next())
{
try{
string s = "_semester";
cout << "semester\t:" << res->getString(s) << endl;
}
catch (exception e)
{
cout<< e.what();
}
}
if (conn != 0)
{
delete conn;
}
system("pause");
}
3、
认证高级PHP讲师