Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
bool insertIntoTable(string database,string table, string query){ 
                        cout<<"Entering insertIntoTable"<<endl;
                        bool allIsWell=false;
                        try {
                                sql::Driver *driver;
                                sql::Connection *con;
                                sql::Statement *stmt;
                                driver = get_driver_instance();
                                con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
                                con->setSchema(database);
                                stmt = con->createStatement();
                                stmt->executeQuery("insert into trade values (100)");
                                delete stmt;
                                delete con;
                                allIsWell=true;
                        } catch (sql::SQLException &e) {
                                cout << "# ERR: SQLException in " << __FILE__<<endl;
                                cout << "# ERR: " << e.what()<<endl;
                                cout << " (MySQL error code: " << e.getErrorCode()<<endl;
                                cout << ", SQLState: " << e.getSQLState() << " )" << endl;                
                        }
                        cout<<"Exiting insertIntoTable"<<endl;
                        return allIsWell;
                }


Why getting the exception and how to fix it?

What I have tried:

This programs inserts successfully into table but gives exception : 
    
    # ERR: SQLException in DBManager.cpp
    # ERR: 
     (MySQL error code: 0
     SQLState: 00000 )
Posted
Updated 16-Dec-20 22:00pm
Comments
Richard Deeming 17-Dec-20 3:45am
   
You need to examine the exception to get the full details of the error. As it stands, you're just guessing blindly at what the problem might be.
Dev Parzival 17-Dec-20 3:52am
   
@Richard Deeming
Entering insertIntoTable
# ERR: SQLException in DBManager.cpp
# ERR:
(MySQL error code: 0
, SQLState: 00000 )
Exiting insertIntoTable

this is the error msg which I have got cant tell exactly where's the issue.
Richard MacCutchan 17-Dec-20 3:58am
   
Use the debugger to step through the code and see exactly which statement throws the exception. Given the fact that all the exception values are zero, it could well be a failure of assignment to any of the first three pointers.
Dev Parzival 17-Dec-20 4:00am
   
@Richard thanks I will try to debug now but it runs successfully and also inserts into table

1 solution

executeQuery returns a result set, which you are not handling - so when you try to delete everything else, it finds an open result set on the connection and fails.

You use executeQuery when you want to fetch data from the DB, not add it: use execute instead.
   
v2
Comments
Dev Parzival 17-Dec-20 4:40am
   
@OriginalGiff thanks a lot
OriginalGriff 17-Dec-20 4:50am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900