我需要一些帮助。我有这个代码(下面),将数据添加到MySQL表,然后返回相同的表。代码运行正常,当我运行它时,它将列添加到MySQL表但它停止,错误:
SQL error. Error message:
字面上一片空白。如果我在SELECT
中使用INCLUDE
语句而不是executeQuery()
语句,它运行没有问题且没有错误消息,只显示我的表(或部分表)。我错过了什么?
我正在使用Visual Studios 2015和MySQL Server。
最终目标是使用C ++将API与SQL表连接,以根据特定的时间跨度记录数据。这是最初的步骤之一,只是为了确保我可以正确地将MySQL与C ++链接起来。
很抱歉,如果这篇文章写得不好,第一次来这里,绝对不是一位经验丰富的程序员...另外,我调查了其他主题,但由于这是如此具体,我找不到它们有用。
// 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;
}
提前致谢
答案 0 :(得分:0)
检查一下:
排队:
res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)");
您正在进行错误的字符串连接,+(加号)运算符不会以这种方式工作,代码不会连接字符串,而是添加指针。
只需简单地替换这种方式再试一次:
#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)");
答案 1 :(得分:0)
INSERT不是查询。尝试使用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示例here以获取该概念的示例。
您也可以尝试使用execute(),如this Stackoverflow问题所示。函数execute()用于通用SQL命令,但在返回值中可能不像更多指定函数那样冗长(它返回一个布尔值)。