我的代码出了什么问题?我正在使用mysql连接器来处理mysql数据库。在构建阶段,一切看起来都很酷,但当我运行代码时,我得到了这个错误:
ERROR: SQLException in /programs/Mysql/main.cpp (main) on line 24
ERROR: The connection is in autoCommit mode (MySQL error code: 0, SQLState: )这是我的完整代码:
#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;
}有什么想法吗?
发布于 2015-08-20 04:21:54
由于在项目的Makefile中包含了错误的头文件,我实际上遇到了这个问题。
在将includes指向正确的MySQL发行版标题之后,我再也没有收到这个错误。
发布于 2015-01-19 01:28:14
“连接处于autoCommit模式”,因此您无法启动事务。
因此,找到将autoCommit设置为false的方法。
autoCommit模式是mySql的默认模式,这意味着每个语句都是它自己的事务。
https://stackoverflow.com/questions/28012620
复制相似问题