c mysql query 错误_C executeQuery()错误显示表中的MySQL数据

我需要一些帮助 . 我有这个代码(下面),将数据添加到MySQL表,然后返回相同的表 . 代码运行正常,当我运行它时,它将列添加到MySQL表但它停止,错误:

SQL error. Error message:

字面上一片空白 . 如果我在 executeQuery() 中使用 SELECT 语句而不是 INCLUDE 语句,它运行没有问题且没有错误消息,只显示我的表(或部分表) . 我错过了什么?

我正在使用Visual Studios 2015和MySQL Server .

最终目标是使用C将API与SQL表连接,以根据特定的时间 Span 记录数据 . 这是最初的步骤之一,只是为了确保我可以正确地将MySQL与C链接起来 .

很抱歉,如果这篇文章写得不好,第一次在这里,绝对不是一个有经验的程序员...另外,我调查了其他线程,但由于这是如此具体,我找不到它们有用 .

// Standard C++ includes

#include

#include

#include

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;

}

提前致谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值