我是cpp的新手。但到目前为止,我所做的一切我都能做到。只是做一个小游戏,我可以移动字符和存储到数据库(mysql)的信息。我以前的代码很简单,但没有添加的空间。我试图为我的所有sql函数创建一个类,并将sql字符串传递给它们。我现在的代码是。。。
# include "pch.h"
# include "function_include.h"
class CHARACTER_STATS
{
public:
string get_connection(string query_receive)
try
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
//Create connection
driver = get_driver_instance();
con = driver->connect("localhost:3306", "root", "Jsafley1");
//Connect to the MySQL Server
con->setSchema("stats");
//prepare for query request
stmt = con->createStatement();
//make the query dynamic to a variable. Can be passed by any
function.
res = stmt->executeQuery(query_receive);
}
catch (sql::SQLException &e)
{
//cout << "#ERR: SQLException in " << __FILE__;
//cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
//cout << "#ERR: " << e.what();
//cout << ", SQLState: " << e.getSQLState() << " )" << endl;
cout << "Error here";
}
}
private:
};
string character_create()
{
try {
string name, health, action, mind, xaxis, yaxis;
cout << "Enter your desired character name. " << endl;
cin >> name;
cout << "\n You have 3 stats and 1000 pints to devide into each stat how
you wish" << endl;
cout << "Set your Health" << endl;
cin >> health;
cout << "Set your Action" << endl;
cin >> action;
cout << "Set your Mind" << endl;
cin >> mind;
string sendQuery;
sendQuery = "INSERT INTO character_stats(player, health, actions, mind) VALUES('" +
name + "', '" +
health + "', '" +
action + "', '" +
mind + "')";
CHARACTER_STATS postQuery;
postQuery.get_connection(sendQuery);
}
catch (sql::SQLException &e)
{
cout << "Inserted but error... ";
}
}
另外,我正在使用visualstudio社区、mysql的c++连接器和boost。
要放入数据库的信息被插入。但是它停止程序并抛出这个异常…异常
这个错误会在我的代码末尾弹出。就在它应该返回main()函数之前。
我错过什么了吗?或者做错事?
谢谢你的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!