C++ Mysql++,总是连接失败!

发布于 2024-07-10 12:39:47 字数 886 浏览 8 评论 0原文

步骤:
- 安装了Mysql Server 2005
- 下载了Mysql++,构建了调试版和发布版。
- 运行 install.hta 并选择一个目录
- 在 MSVC++ 2008 中添加了库/包含目录
- 在我的应用程序中包含 mysql++.h
- 将 .dll 文件(libMYSQL.dll 和 mysqlpp.dll 和 mysqlpp_d.dll)移至 Debug 文件夹。

相关代码:

#include "mysql++.h"

class Database {

private:

    mysqlpp::Connection* conn;

public:

    ~Database();
    bool Connect(char* ip, char* user, char* pass, char* db);

};

bool Database::Connect(char* ip, char* user, char* pass, char* db) {
    conn = new mysqlpp::Connection(false);
    return conn->connect(db, ip, user, pass);
}

Database::~Database() {
    if(conn) {
        delete[] conn;
    }
}

问题:

Database db;
db.Connect("127.0.0.1", "root", "mypassword", "mydb");

这将总是返回到 false,即使我我使用与 MySQL 管理员完全相同的凭据并正确登录。

帮助 :(

Steps:
- Installed Mysql Server 2005
- Downloaded Mysql++, built both debug and release version.
- Ran install.hta and selected a directory
- Added library/include directory in MSVC++ 2008
- Included mysql++.h in my application
- Moved .dll files (libMYSQL.dll and mysqlpp.dll and mysqlpp_d.dll) to Debug folder.

Relevant code:

#include "mysql++.h"

class Database {

private:

    mysqlpp::Connection* conn;

public:

    ~Database();
    bool Connect(char* ip, char* user, char* pass, char* db);

};

bool Database::Connect(char* ip, char* user, char* pass, char* db) {
    conn = new mysqlpp::Connection(false);
    return conn->connect(db, ip, user, pass);
}

Database::~Database() {
    if(conn) {
        delete[] conn;
    }
}

Problem:

Database db;
db.Connect("127.0.0.1", "root", "mypassword", "mydb");

This will always return to false, even though I am using the exact same credentials with MySQL Administrator and logging in correctly.

Help :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-07-17 12:39:47

我不会提出这个指针:

mysqlpp::Connection* conn;

只需使其成为班级的普通成员即可。

mysqlpp::Connection conn;

这有几个优点。
但对您来说最重要的是您将避免浅复制问题。

4 规则:

如果一个对象是 RAW 指针的所有者,那么您需要定义以下 4 个成员以确保处理内存管理正确:

* Constructor
* Copy Constructor
* Assignment Operator
* Destructor

这是因为如果你不定义它们,编译器会自动为你生成上述方法。 在大多数情况下,这些方法都有效,但是如果您的对象包含您拥有的原始指针(即您删除它),那么编译器生成的这些方法的版本将会出现严重错误。

I would not make this pointer:

mysqlpp::Connection* conn;

Just make it a normal member of the class.

mysqlpp::Connection conn;

This has several advantages.
But the most important to you is that you will avoid the shallow copy problem.

Rule of 4:

If an object is the owner of a RAW pointer then you need to define the following 4 members to make sure you handle the memory management correctly:

* Constructor
* Copy Constructor
* Assignment Operator
* Destructor

This is because if you do not define them the compiler will automatically generate the above methods for you. In most situations these work, but if your object contains a RAW pointer that you own (ie you delete it) then things will go horibly wrong witht he compiler generated version of these methods.

以酷 2024-07-17 12:39:47

另外,我会使用字符串而不是字符指针:

bool Database::Connect(char* ip, char* user, char* pass, char* db)

尝试:

Database::Database(std::string const& ip,
                   std::string const& user,
                   std::string const& pass,
                   std::string const& db)

注意,通过将代码从方法 Connect() 移至 constructor(),您可以使对象始终有效。

Also I would use strings rather than char pointers:

bool Database::Connect(char* ip, char* user, char* pass, char* db)

Try:

Database::Database(std::string const& ip,
                   std::string const& user,
                   std::string const& pass,
                   std::string const& db)

Note by moving the code from the method Connect() to the constructor() you can make the object always valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文