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

相关推荐
马剑威(威哥爱编程)4 分钟前
分布式Python计算服务MaxFrame使用心得
开发语言·分布式·python·阿里云
翱翔-蓝天8 分钟前
Python知识图谱框架
开发语言·python·知识图谱
CodeMartain11 分钟前
stream流的toMap
java·开发语言
lsx20240615 分钟前
Lua 函数
开发语言
ling1s21 分钟前
C#核心(18)面向对象多态vob
java·开发语言·c#
HD24360883622 分钟前
【mysql】id主键列乱了之后,重新排序(可根据日期顺序)
数据库·sql·mysql
皓木.31 分钟前
苍穹外卖——准备工作
java·数据库·mybatis
qq_4489410837 分钟前
6、mysql的MHA故障切换
数据库·mysql
美美的海顿1 小时前
springboot基于Java的校园导航微信小程序的设计与实现
java·数据库·spring boot·后端·spring·微信小程序·毕业设计
会飞的爱迪生1 小时前
mysql主从同步延迟原因分析
数据库·mysql