#include "StdAfx.h"
#include <stdlib.h>
#include <iostream>
#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>
using namespace std;
int main(void){
cout << endl;
try{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
driver = get_driver_instance();
con = driver->connect("REMOVED", "REMOVED", "REMOVED");
con->setSchema("REMOVED");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT username FROM player WHERE id=1");
cout << "Username: " << res->getString("username") << endl;
delete res;
delete con;
cout << "Done.";
system("pause");
}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() << endl;
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
system("pause");
}
return 0;
}变成了..。http://i.imgur.com/cIVnl.png
发生了什么?:(这只是我使用C++的第二天,所以请原谅我糟糕的格式化编码和其他nooby错误。这总是在我得到一个未处理的异常之前出现。
发布于 2012-03-07 04:39:17
我相信您使用的API会返回std::string对象。
在这种情况下,您可能希望在代码顶部添加一个字符串,以便为std::#include <sstream>类定义好一个'<<‘运算符。
我不确定这个解决方案,因为我希望你得到一个编译错误,以某种方式暗示没有运算符'<<‘找不到。
试着把它放在你的代码中,然后立即从main()调用它,看看流运算符是否正常工作。注意,它将使您的程序退出。
void SimpleStrings()
{
std::string str("String in a std::string");
const char *psz = "const-char-str";
std::cout << "std::string: " << str << std::endl << "const char *: " << psz << std::endl;
exit(1);
}https://stackoverflow.com/questions/9590913
复制相似问题