C++ Mysql++,总是连接失败!
步骤:
- 安装了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会提出这个指针:
只需使其成为班级的普通成员即可。
这有几个优点。
但对您来说最重要的是您将避免浅复制问题。
4 规则:
如果一个对象是 RAW 指针的所有者,那么您需要定义以下 4 个成员以确保处理内存管理正确:
这是因为如果你不定义它们,编译器会自动为你生成上述方法。 在大多数情况下,这些方法都有效,但是如果您的对象包含您拥有的原始指针(即您删除它),那么编译器生成的这些方法的版本将会出现严重错误。
I would not make this pointer:
Just make it a normal member of the class.
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:
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.
另外,我会使用字符串而不是字符指针:
尝试:
注意,通过将代码从方法 Connect() 移至 constructor(),您可以使对象始终有效。
Also I would use strings rather than char pointers:
Try:
Note by moving the code from the method Connect() to the constructor() you can make the object always valid.