2

I have this c++ code that works fine, i can read from the tables and write to the tables:

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;
}

So, this inserts to the table but then i get this error message

SQL error. Error message: sh: 1: pause: not found

This doesn't happen if i use the "select".

Also i know that this question was already asked here but unfortunately it has no answer so i'm asking again.

John123
  • 122
  • 1
  • 2
  • 11
  • Are you on a Windows computer or not? `system("pause")` will not work outside Windows. – Arnav Borborah Feb 16 '18 at 20:41
  • @ArnavBorborah no i'm using linux – John123 Feb 16 '18 at 20:43
  • well i don't think its the problem, because even if i comment out the whole catch statement. The still exits – John123 Feb 16 '18 at 20:45
  • Can you print error code: `cout << "MySQL error code: " << e.getErrorCode();`? – ks1322 Feb 16 '18 at 20:52
  • @ks1322 This is the result **SQL error. Error message: 0** – John123 Feb 16 '18 at 20:59
  • Looks related to https://stackoverflow.com/q/6903383/72178. Try `stmt->execute("INSERT INTO users (fName, lName, age) VALUES ('fname', 'lname', 25)");`. – ks1322 Feb 16 '18 at 21:15
  • @ks1322 execute() returned this error: `error: cannot convert ‘bool’ to ‘sql::ResultSet*’ in assignment ute("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` – John123 Feb 16 '18 at 21:31
  • 1
    @Arnav: `echo 'alias pause=read' >> ~/.bashrc` Now it does :D – Lightness Races in Orbit Feb 17 '18 at 01:05

2 Answers2

3

Your question looks related to MySQL Query executes but throws exception.

executeQuery() assumes that sql query should return sql::ResultSet but your INSERT INTO query does not. You can use execute() instead, which returns true or 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);
}
ks1322
  • 33,961
  • 14
  • 109
  • 164
2

An INSERT is not a query. Try using executeUpdate() instead of executeQuery(). Look at the official MySQL example here.

Replace this line

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

with the following lines (you may need a new .h file):

sql::PreparedStatement *pstmt;

pstmt = con->prepareStatement("INSERT INTO users (fName, lName, age) 
VALUES ('fname', 'lname', 25)");
res = pstmt->executeUpdate();
delete pstmt;

You may also try using execute(), as shown in this Stackoverflow question. The function execute() is used for generic SQL commands, but may not be as verbose in its return value as more specified functions (it returns a boolean).

JCollier
  • 1,102
  • 2
  • 19
  • 31
  • executeUpdate() returned this error: `error: invalid conversion from ‘int’ to ‘sql::ResultSet*’ [-fpermissive] res = stmt->executeUpdate("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` while execute() returned this error: `error: cannot convert ‘bool’ to ‘sql::ResultSet*’ in assignment ute("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` – John123 Feb 16 '18 at 21:28
  • I just edited my answer to have a code example. It might get around that error. – JCollier Feb 16 '18 at 22:01