Reputation: 1029
what's wrong with my code? I'm using mysql connector to work with mysql database. everything seems cool in build phase but when I run my code I get this error :
ERROR: SQLException in /programs/Mysql/main.cpp (main) on line 24
ERROR: The connection is in autoCommit mode (MySQL error code: 0, SQLState: )
this is my complete code :
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
using namespace std;
using namespace sql;
int main(void) {
try {
Driver *driver;
Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "123");
con->setAutoCommit(0);
con->setSchema("webscope");
delete con;
} catch (SQLException &e) {
cout << "ERROR: SQLException in " << __FILE__;
cout << " (" << __func__ << ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl;
if (e.getErrorCode() == 1047) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
}
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
any idea?
Upvotes: 1
Views: 1405
Reputation: 3739
I actually ran into this issue as a result of including the wrong header files in my project's Makefile.
In my Makefile, I was including headers for a Solaris release of MySQL, but was compiling on Linux. After pointing the includes to the correct MySQL release headers, I didn't get this error anymore.
Upvotes: 1
Reputation: 5545
"The connection is in autoCommit mode" so you can't start a transaction.
So find the method to set autoCommit to false.
autoCommit mode is the default mode for mySql at it means that each statement is its own transaction.
Upvotes: 0