【问题标题】:MySQL Connector/C++ PreparedStatement: forward declaration of ‘class sql::PreparedStatement’MySQL 连接器/C++ PreparedStatement:“class sql::PreparedStatement”的前向声明
【发布时间】:2013-12-14 12:01:52
【问题描述】:

我对 c++ Mysql 连接器有“一点”问题。我想使用 PreparedStatement。我尝试了文档 http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-prepared-statements.html

中的示例

我无法编译此代码。我仍然收到错误:

main.cpp:32:10:错误:无效使用不完整类型“class sql::PreparedStatement” 在 /usr/include/mysql_connection.h:28:0 包含的文件中, 来自 main.cpp:4: /usr/include/cppconn/connection.h:44:7:​​ 错误:‘class sql::PreparedStatement’的前向声明

我使用带有 g++ 4.7.2 的 debian 7。

我已经在谷歌上搜索了几个小时,但我不知道如何解决这个问题。 我会很高兴收到每一个建议。

我的代码:

#include <iostream>
#include <cstdlib>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char** argv) {
    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::PreparedStatement  *prep_stmt;

        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "username", "pass");
        // db name
        con->setSchema("aa");

        prep_stmt = con->prepareStatement("INSERT INTO test(id, name) VALUES (?, ?)");

        // this will couse compilation error
        prep_stmt->setInt(1, 1);

        // this will cause only warning but same message
        delete prep_stmt;

        delete con;
    }
    catch (sql::SQLException &e) {
        std::cout << "ERR: " << e.what() << std::endl;
    }
    return 0;
}

【问题讨论】:

  • 您必须缺少定义 PreparedStatement 的包含。
  • @polkadotcadaver 你是对的!非常感谢,我真的不知道我怎么会错过它。我觉得很尴尬。再次非常感谢。

标签: c++ g++ prepared-statement mysql-connector forward-declaration


【解决方案1】:

缺少一个#include,您必须设置:

#include &lt;cppconn/prepared_statement.h&gt;

【讨论】:

    【解决方案2】:

    问题是由于没有安装mysql驱动,或者没有在编译命令中添加导致的链接问题。

    您可以先安装驱动: sudo apt-get -y install libmysqlcppconn5 libmysqlcppconn-dev

    安装完毕,编译: g++ -o main.cpp -lmysqlcppconn

    使用 IDE,您可以点击此链接在您的 IDE 中添加库。

    MySQL Connector/C++: how to build a client on Linux using NetBeans

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      相关资源
      最近更新 更多