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

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

执行后显示效果:

bash 复制代码
"QSQLITE"
"QODBC"
"QODBC3"
"QPSQL"
"QPSQL7"
connected success to postgresql9.4
"admin"   "1"

注意事项:

bash 复制代码
连接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

bash 复制代码
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

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

相关推荐
努力也学不会java2 分钟前
【Java并发】揭秘Lock体系 -- 深入理解ReentrantReadWriteLock
java·开发语言·python·机器学习
ActionTech4 分钟前
2025 年 9 月《大模型 SQL 能力排行榜》发布,新增 Kimi K2 最新版测评!
数据库·sql·ai·oracle
vxtkjzxt88816 分钟前
自动化脚本矩阵运营
开发语言·php
王严培.25 分钟前
7.MATLAB疑难问题诊疗的技术
开发语言·matlab·信息可视化
wjs202436 分钟前
PHP MySQL 使用 ORDER BY 排序查询
开发语言
爱敲代码的TOM42 分钟前
深入剖析Java通信架构下的三种IO模式2
java·开发语言·架构
lang201509281 小时前
掌握MyBatis Java API:高效操作数据库
java·数据库·mybatis
UWA1 小时前
Unreal开发痛点破解!GOT Online新功能:Lua全监控 + LLM内存可视化!
开发语言·lua·unreal
Mr.wangh1 小时前
Redis作为分布式锁
数据库·redis·分布式
半夏知半秋1 小时前
skynet debug_console控制台中debug指令使用
服务器·开发语言·学习·lua