#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错误。这总是出现在我得到未处理的异常之前。
答案 0 :(得分:0)
您使用的API我相信返回std :: string对象。
在这种情况下,您可能希望在代码顶部添加#include <sstream>
,这样您就可以很好地定义'&lt;&lt;&lt; std :: string类的运算符。
我不确定这个解决方案,因为我希望你得到一个编译错误,某种程度上暗示没有运算符'&lt;&lt;'可以找到。
尝试在代码中粘贴它并立即从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);
}