【发布时间】: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