strange problems using mysql-connector and c++
Dec 10, 2014 at 2:39am UTC
I have been having trouble with this code for a while. It compiles, but I throw an exception which I don't know what to make of.
About halfway down in the last function I threw in a "cout" statement to test the data being sent to the SQL server. It looks exactly right. I can use the exact command in SQL to make the change myself, but I get this when I execute the code.
1 2 3 4
UPDATE USERS SET CREDITS='999999999' WHERE UID='1'
# ERR: SQLException in sqlcomm.cpp(pushData_USERS) on line 60
# ERR: (MySQL error code: 0, SQLState: 00000 )
999999999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
efine DATABASE "TEST"
#define USERNAME "root"
#define PASSWORD "423490769iss"
#define ADDRESS "tcp: //127.0.0.1:3306"
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <string>
#include <sstream>
using namespace std;
string pullData_USERS( string uid, string column ){
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
con = driver->connect( ADDRESS,USERNAME,PASSWORD );
con->setSchema( DATABASE );
stmt = con->createStatement();
ostringstream strstr;
strstr << "SELECT " << column <<" FROM USERS " << " WHERE UID='" << uid << "'" ;
string s = strstr.str();
res = stmt->executeQuery(s.c_str());
while ( res->next())
s = res->getString(column);
return s;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return NULL;
}
void pushData_USERS(string uid, string column, string input){
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = get_driver_instance();
con = driver->connect( ADDRESS,USERNAME,PASSWORD );
con->setSchema( DATABASE );
stmt = con->createStatement();
ostringstream strstr;
strstr << "UPDATE USERS SET " << column << "='" << input <<"' WHERE UID='" << uid<<"'" ;
string s = strstr.str();
cout <<s.c_str()<<endl;
stmt->executeQuery(s.c_str());
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
}
int main(){
pushData_USERS("1" ,"CREDITS" ,"999999999" );
string q = pullData_USERS("1" ,"CREDITS" );
cout <<q<<endl;
}
Dec 10, 2014 at 6:43pm UTC
Just a thought: try using execute() in lieu of executeQuery() since "update" does not produce a result set and executeQuery() expects one.
Dec 11, 2014 at 1:11am UTC
That was it!!
Thanks.
Topic archived. No new replies allowed.