【问题标题】:ERROR: The connection is in autoCommit mode错误:连接处于自动提交模式
【发布时间】:2015-01-18 17:26:05
【问题描述】:

我的代码有什么问题?我正在使用 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;
}

有什么想法吗?

【问题讨论】:

  • 我删除了一些 cmets 以获得更短的代码 sn-p。
  • 很抱歉碰到了这个问题,你解决过这个问题吗?我也有同样的问题。

标签: c++ mysql mysql-connector


【解决方案1】:

我实际上遇到了这个问题,因为我在项目的 Makefile 中包含了错误的头文件。

在我的 Makefile 中,我包含了 MySQL 的 Solaris 版本的标头,但在 Linux 上进行编译。在将包含指向正确的 MySQL 发布标头后,我不再收到此错误。

【讨论】:

    【解决方案2】:

    “连接处于自动提交模式”,因此您无法启动事务。

    所以找到设置autoCommit为false的方法。

    autoCommit 模式是 mySql 的默认模式,它意味着每条语句都是它自己的事务。

    【讨论】:

    • 奇数。此外,如果您开始新事务并且您的代码不这样做,您应该只获得该异常。您确定这是您正在运行的唯一代码吗?
    • 是的。我使用这个简单的代码来测试在 mysql 上运行的查询。但我得到了这个错误。为什么我不能将自动提交设置为 false?
    猜你喜欢
    • 2017-11-02
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多