c++ 调用mysql出错
执行mysql官方给的Connector/C++ 例子出错!
代码如下:
#include<stdlib.h>
#include<iostream>
#include<sstream>
#include<stdexcept>
#include<vector>
#include<string>
#include"mysql_connection.h"
#include<cppconn/driver.h>
#include<cppconn/exception.h>
#include<cppconn/resultset.h>
#include<cppconn/statement.h>
#include<cppconn/prepared_statement.h>
#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "booksql"
#define EXAMPLE_PASS "booksql"
#define EXAMPLE_DB "world"
using namespace std;
int main(int argc,const char **argv)
{
string url(argc>=2 ? argv[1]:EXAMPLE_HOST);
const string user(argc>=3 ? argv[2]:EXAMPLE_USER);
const string pass(argc>=4 ? argv[3]:EXAMPLE_PASS);
const string database(argc>=5 ? argv[4]:EXAMPLE_DB);
cout<<"Connector/C++ tutorial framework..."<<endl;
cout<<endl;
try{
sql::Driver * driver = get_driver_instance();
std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
con->setSchema(database);
std::auto_ptr< sql::PreparedStatement > pstmt;
std::auto_ptr< sql::ResultSet > res;
pstmt.reset(con->prepareStatement("CALL get_data()"));
res.reset(pstmt->executeQuery());
do {
res.reset(pstmt->getResultSet());
while (res->next()) {
cout<< "Name: " << res->getString("Name")
<< " Population: " << res->getInt("Population")
<< endl;
}
} while (pstmt->getMoreResults());
}catch(sql::SQLException &e)
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* Use what() (derived from std::runtime_error) to fetch the error message */
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return EXIT_FAILURE;
}
cout<<"Done."<<endl;
return EXIT_SUCCESS;
}
出现错误: Commands out of sync; you can't run this command now!
恳请高手多多指点!多谢!
------解决方案--------------------http://blog.csdn.net/luketty/article/details/5745000
------解决方案--------------------c++不支持*
------解决方案--------------------2楼正解。