【问题标题】:postgresql connection error with qt creator in windowswindows中qt创建者的postgresql连接错误
【发布时间】:2013-06-11 17:58:31
【问题描述】:

我的 qui 上有一个按钮。当我点击它时,它必须根据连接信息连接数据库。

这是我的代码:

void MainWindow::on_pushButton_clicked(){
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("Assignment3");
    db.setUserName("postgres");
    db.setPassword("password");
    db.setPort(5432);

    bool ok = db.open();
    if(ok != true)
    {
        QMessageBox::information(this,"Connection","Connection Failed!") ;
    }
    else
    {
        QMessageBox::information(this,"Connection","Connection OK!") ;

        QSqlQueryModel model;
        model.setQuery("select * from students ");

        //ui->tableView->setModel(&model);
        QMessageBox::information(this,"Information","This Message box is needed in       order to see the rendered tableview!") ;

    }

}

主机名、数据库名、用户名、密码和端口都正确,并且我在 postgresql 中的数据库处于活动状态,其中不包含表。

感谢任何帮助。提前致谢。

【问题讨论】:

  • 消息框说什么?
  • “连接失败”。问题是没有 qt 的 qpsql 驱动程序。
  • 您的 .pro 文件中有 win32:LIBS += libpqdll.lib 吗?你有支持 postgresql 的 Qt 吗?
  • 尝试db.lastError().text();了解更多信息
  • QSqlDatabase:未加载 QPSQL 驱动程序 QSqlDatabase:可用驱动程序:QSQLITE QODBC QODBC3

标签: database qt postgresql connection


【解决方案1】:

如果您缺少驱动程序(如 cmets 建议的那样),那么您应该考虑获取它。

例如,在 Debian 上,您会:

apt-get install  libqt4-sql-psql

在 Fedora 上你会使用类似的 yum 命令

但是,在不了解您的平台的情况下,我无法建议具体的步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2015-08-13
    • 2016-05-14
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多