Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++显示从表中显示mysql数据的错误_C++_Mysql_Sql_Visual Studio_Visual Studio 2015 - Fatal编程技术网

C++显示从表中显示mysql数据的错误

C++显示从表中显示mysql数据的错误,c++,mysql,sql,visual-studio,visual-studio-2015,C++,Mysql,Sql,Visual Studio,Visual Studio 2015,我需要一些帮助。我有下面的代码,将数据添加到MySQL表中,然后返回同一个表。代码运行得很好,当我运行它时,它将列添加到MySQL表中,但它停止了,错误如下: SQL error. Error message: 简直是空白。如果我在executeQuery中使用SELECT语句而不是INCLUDE语句,它运行时不会出现问题,也不会出现错误消息,只会显示我的表或其中的一部分。我错过了什么 我正在使用Visual Studio 2015和MySQL服务器 最终目标是使用C++连接API和SQL表

我需要一些帮助。我有下面的代码,将数据添加到MySQL表中,然后返回同一个表。代码运行得很好,当我运行它时,它将列添加到MySQL表中,但它停止了,错误如下:

SQL error. Error message: 
简直是空白。如果我在executeQuery中使用SELECT语句而不是INCLUDE语句,它运行时不会出现问题,也不会出现错误消息,只会显示我的表或其中的一部分。我错过了什么

我正在使用Visual Studio 2015和MySQL服务器

最终目标是使用C++连接API和SQL表,根据特定的时间段记录数据。这是第一步,只是为了确保我能正确地C++连接MySQL。 抱歉,如果这篇文章写得不好,第一次在这里,肯定不是一个有经验的程序员。。。此外,我还研究了其他线程,但由于这是如此具体,我找不到它们的帮助

// Standard C++ includes
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

// Include the Connector/C++ headers
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"

// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")

// Specify our connection target and credentials
const string server = "127.0.0.1:3306";
const string username = "root";
const string password = "root";
const string database = "dbex"; 
const string table = "tbex";

int main()
{
    sql::Driver     *driver; // Create a pointer to a MySQL driver object
    sql::Connection *dbConn; // Create a pointer to a database connection object
    sql::Statement  *stmt;   // Create a pointer to a Statement object to hold our SQL commands
    sql::ResultSet  *res;    // Create a pointer to a ResultSet object to hold the results of any queries we run

                             // Try to get a driver to use to connect to our DBMS
    try
    {
        driver = get_driver_instance();
    }
    catch (sql::SQLException e)
    {
        cout << "Could not get a database driver. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    // Try to connect to the DBMS server
    try
    {
        dbConn = driver->connect(server, username, password);
        dbConn->setSchema(database);
    }
    catch (sql::SQLException e)
    {
        cout << "Could not connect to database. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }
    stmt = dbConn->createStatement();

    // Try to query the database
    try
    {
        //stmt->execute("USE " + database);              // Select which database to use. Notice that we use "execute" to perform a command.
        res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 
        res = stmt->executeQuery("SELECT * FROM cars"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back

    }
    catch (sql::SQLException e)
    {
        cout << "SQL error. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

// While there are still results (i.e. rows/records) in our result set...
    while (res->next())
    {
        cout << res->getString(1) << endl;
    }

    delete res;
    delete stmt;
    delete dbConn;

    system("pause");
    return 0;
}
提前感谢

检查此项:

一致:

res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)");
您正在进行错误的字符串连接,+plus运算符不是这样工作的,代码不连接字符串,而是添加指针

只需以这种方式更换,然后重试:

#define TABLE "tbex"// put this in top of cpp file
......
res = stmt->executeQuery("INSERT INTO " TABLE "(Brand, Model, Power, `Last Used`
,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)");

插入不是查询。尝试使用executeUpdate而不是executeQuery

更换这条线

res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 
对于以下行,您可能需要一个额外的.h文件:

sql::PreparedStatement *pstmt;

pstmt = con->prepareStatement("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)");
res = pstmt->executeUpdate();
delete pstmt;
请看官方的MySQL示例,了解该概念的示例


您也可以尝试使用execute,如Stackoverflow问题所示。函数execute用于一般的SQL命令,但其返回值可能没有返回布尔值的更多指定函数那么详细。

谢谢您的回复。我尝试了这个方法,但遇到了与以前相同的问题:行被添加到表中,但它仍然显示一条空白错误消息,并在添加后停止代码