插入MYSQL数据库中的表后出现错误消息 我有这个C++代码,它工作得很好,我可以从表中读取并写到表: int main() { // 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); } catch (sql::SQLException e) { cout << "Could not connect to database. Error message: " << e.what() << endl; system("pause"); exit(1); } stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on // Try to query the database try { stmt->execute("USE test"); // Select which database to use. Notice that we use "execute" to perform a command. res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back //res = stmt->executeQuery("SELECT * FROM users"); //return 0; } catch (sql::SQLException e) { cout << "SQL error. Error message: " << e.what() << endl; system("pause"); exit(1); } sql::ResultSetMetaData *res_meta = res -> getMetaData(); int columns = res_meta -> getColumnCount(); // While there are still results (i.e. rows/records) in our result set... while (res->next()) { for (int i = 1; i <= columns; i++) { cout << res->getString(i) << " | " ; } cout << endl; } delete res; delete stmt; delete dbConn; //system("pause"); return 0; } intmain() { //尝试使用驱动程序连接到我们的DBMS 尝试 { driver=get_driver_instance(); } 捕获(sql::SQLE异常) { cout

插入MYSQL数据库中的表后出现错误消息 我有这个C++代码,它工作得很好,我可以从表中读取并写到表: int main() { // 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); } catch (sql::SQLException e) { cout << "Could not connect to database. Error message: " << e.what() << endl; system("pause"); exit(1); } stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on // Try to query the database try { stmt->execute("USE test"); // Select which database to use. Notice that we use "execute" to perform a command. res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back //res = stmt->executeQuery("SELECT * FROM users"); //return 0; } catch (sql::SQLException e) { cout << "SQL error. Error message: " << e.what() << endl; system("pause"); exit(1); } sql::ResultSetMetaData *res_meta = res -> getMetaData(); int columns = res_meta -> getColumnCount(); // While there are still results (i.e. rows/records) in our result set... while (res->next()) { for (int i = 1; i <= columns; i++) { cout << res->getString(i) << " | " ; } cout << endl; } delete res; delete stmt; delete dbConn; //system("pause"); return 0; } intmain() { //尝试使用驱动程序连接到我们的DBMS 尝试 { driver=get_driver_instance(); } 捕获(sql::SQLE异常) { cout,c++,mysql,C++,Mysql,插入不是查询。请尝试使用executeUpdate()而不是executeQuery()。查看官方的MySQL示例 更换这条线 res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get resul

插入不是查询。请尝试使用executeUpdate()而不是executeQuery()。查看官方的MySQL示例

更换这条线

res = stmt->executeQuery("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
使用以下行(您可能需要一个新的.h文件):


您也可以尝试使用execute(),如Stackoverflow问题中所示。函数execute()用于一般SQL命令,但其返回值可能不如其他指定函数(它返回布尔值)详细。

您的问题看起来与之相关

executeQuery()
假定sql查询应返回
sql::ResultSet
,但您的
插入到
查询中不返回。您可以改为使用
execute()
,该查询返回true或false:

try
{
    stmt->execute("USE test");
    stmt->execute("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)");
}
catch (sql::SQLException e)
{
    cout << "SQL error. Error message: " << e.what() << endl;
    exit(1);
}
试试看
{
stmt->执行(“使用测试”);
stmt->execute(“插入用户(fName,lName,age)值('fName','lName',25)”);
}
捕获(sql::SQLE异常)
{

您是否在Windows计算机上?
系统(“暂停”)
无法在Windows之外工作。@ArnavBorborah不,我使用的是linuxwell,我认为这不是问题所在,因为即使我注释掉了整个catch语句。仍然存在的错误代码可以打印:
cout@Arnav:
echo'alias pause=read'>~/.bashrc
现在可以:DexecuteUpdate()返回此错误:
错误:从“int”到“sql::ResultSet*”[-fppermission]res=stmt->executeUpdate(“插入用户(fName,lName,age)值('fName',lName',25)”)的转换无效;//执行查询并获取结果。请注意,在执行()时使用“executeQuery”返回结果返回此错误:
error:无法将赋值ute中的'bool'转换为'sql::ResultSet*'(“插入用户(fName,lName,age)值('fName','lName',25)”);//执行查询并获取结果。请注意,我们使用了“executeQuery”为了返回结果
我刚刚编辑了我的答案,以获得一个代码示例。它可能会绕过这个错误。
try
{
    stmt->execute("USE test");
    stmt->execute("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)");
}
catch (sql::SQLException e)
{
    cout << "SQL error. Error message: " << e.what() << endl;
    exit(1);
}