-
配置环境变量
C:\Program Files\PostgreSQL\10\bin;C:\Program Files\PostgreSQL\10\lib;
-
新建
qt
项目勾选SQL
支持 -
复制
sqldrivers
目录,目录名固定
-
具体代码示例
#include "QtGuiApplication1.h"
#include "qsqldatabase.h"
#include "qsqlquery.h"
#include "qdebug.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("postgis_24_sample");
db.setPort(5432);
db.setUserName("postgres");
db.setPassword("admin");
bool ok = db.open();
int b = ok;
QSqlQuery query;
query.exec("Select * from \"Pipes\"");
while (query.next())
{
QString id = query.value(1).toString();
qDebug() << id;
}
}
网友评论