qt 5.15.2连接postgresql9.4数据库功能

发布时间:2023年12月19日

qt 5.15.2连接postgresql9.4数据库功能

执行后显示效果:

"QSQLITE"
"QODBC"
"QODBC3"
"QPSQL"
"QPSQL7"
connected success to postgresql9.4
"admin"   "1"

注意事项:

连接postgresql9.4数据库需要copy下列文件到执行程序所在目录下
-----------qt自带的-----------------
qsqlpsqld.dll

-----------postgresql的-------------------
libpq.dll
ssleay32.dll
libeay32.dll
libintl.dll

---------.pro 添加行-------------------
QT       += sql

# custom add postgresql .h and .lib file path  by hsg
INCLUDEPATH += "D:/PostgreSQL/9.4/include"
LIBS +="D:/PostgreSQL/9.4/lib/libpq.lib"

.pro

QT       += core gui opengl 3dcore 3drender 3dinput 3dextras
QT       += sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    dialog_fbx2glb.cpp \
    dialog_osgb_to_b3dm.cpp \
    leftorbitcameracontroller.cpp \
    main.cpp \
    mainwindow.cpp \
    qleftcameracontroller.cpp \
    scene.cpp

HEADERS += \
    LeftOrbitCameraControllerPrivate.h \
    dialog_fbx2glb.h \
    dialog_osgb_to_b3dm.h \
    leftorbitcameracontroller.h \
    mainwindow.h \
    qleftcameracontroller.h \
    scene.h

FORMS += \
    dialog_fbx2glb.ui \
    dialog_osgb_to_b3dm.ui \
    mainwindow.ui

# custom add postgresql .h and .lib file path  by hsg
INCLUDEPATH += "D:/PostgreSQL/9.4/include"
LIBS +="D:/PostgreSQL/9.4/lib/libpq.lib"

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

DISTFILES += \
    images/211.ico \
    images/Clear2.ico \
    images/File.ico \
    images/Open.ico \
    images/opendir.png \
    images/openfile.png \
    readme.txt

man.cpp

#include "mainwindow.h"

#include <QApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <libpq-fe.h>

void printSqlDrivers();  //先定义
void testConnectSQL();

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() +"/plugins");
    //QApplication::addLibraryPath(QApplication::applicationDirPath() +"/plugins");
    //MainWindow w;
    //w.show();

    printSqlDrivers();   //调用
    testConnectSQL();    //测试连接postgresql9.4

    return a.exec();
}

//后函数实现
void printSqlDrivers()
{
    QStringList drivers=QSqlDatabase::drivers();
    foreach(QString driver,drivers)
    {
        qDebug()<<driver;
    }
    /*
    "QSQLITE"
    "QODBC"
    "QODBC3"
    "QPSQL"
    "QPSQL7"
    */
    //上面输出有QPSQL关键字才能继续操作testConnectSQL()

}
void testConnectSQL()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("192.168.30.33");
    db.setDatabaseName("pg-sys");
    db.setPort(5432);
    db.setUserName("postgres");
    db.setPassword("1");
    bool ok=db.open();
    if(ok){
        //QMessageBox::information(nullptr,"infor","success");
        qDebug()<<"connected success to postgresql9.4";
    }
    else
    {
        //QMessageBox::information(nullptr,"infor","open failed");
        qDebug()<<"open failed";
        //报错可能为
        //1.copy sql相关的库到可执行目录下,
        //或2.db的连接有错误的参数
        //或3.qt的psql驱动版本不兼容(需重新编译qt 5.15.2安装自带源码下插件psql.pro工程 需引用psql安装libpq.dll库)
    }
    QSqlQuery q=db.exec("select * from sys_user order by user_id asc");
    while(q.next())
    {
        QString userId=q.value("user_id").toString();
        QString userName=q.value("user_name").toString();
        qDebug()<<userName<<" "<<userId;
    }

    db.close();

}

本blog地址:https://blog.csdn.net/hsg77

文章来源:https://blog.csdn.net/hsg77/article/details/135016563
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。