gpt4 book ai didi

c++ - 如何将 MongoDB 与 Qt C++ 连接起来?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:24 56 4
gpt4 key购买 nike

我正在尝试将 MongoDB 与 QT c++ 连接起来。我编写了运行时没有任何错误的简单应用程序。

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

try
{
connectMongo();
qDebug() << "Connection done";
}
catch (const mongo::DBException &e)
{
qDebug() << "Failed : " << e.what();
}

}

void MainWindow::connectMongo()
{
mongo::DBClientConnection c;
c.connect("127.0.0.1");
}

但是,当我键入“127.0.0.1”或“localhost”时,它无法连接,并且在调试窗口中显示:

The program has unexpectedly finished.

当我键入另一个值时,例如 c.connect("0.0.0.0");

它进入 catch block ,异常是:

Failed :  can't connect couldn't connect to server 0.0.0.0:27017

据我所知,它使用正确的端口号工作,该端口号在 MongoDB API 上描述为默认值。那么为什么我不能使用 MongoDB 并且无法连接它呢?

这是服务的输出;

[mg@mg-CentOS ~]$ service mongod status
mongod (pid 4151) is running...

最佳答案

QxOrm library现在支持 MongoDB 数据库。这是文档的链接:https://www.qxorm.com/qxorm_en/manual.html#manual_95

关于c++ - 如何将 MongoDB 与 Qt C++ 连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586638/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com