首页 > 解决方案 > 插入 MYSQL 的 C++ 问题:插入数据库但抛出异常错误

问题描述

我是 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... ";
}
}

此外,我正在使用 Visual Studio Community、C++ Connector for MySQL 和 Boost。

这个要放入数据库的信息被插入。但随后它停止程序并引发此异常...异常

错误在我的代码的最后弹出。就在它应该返回到 main() 函数之前。

我错过了什么吗?或做错了什么?

谢谢您的帮助。

标签: c++mysqlclassexception

解决方案


推荐阅读