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

相关推荐
九皇叔叔30 分钟前
【9】PostgreSQL 之 vacuum 死元组清理
数据库·postgresql
L_autinue_Star1 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
风雅的远行者1 小时前
mysql互为主从失效,重新同步
数据库·mysql
元气小嘉1 小时前
前端技术小结
开发语言·前端·javascript·vue.js·人工智能
励志的大鹰哥1 小时前
V少JS基础班之第七弹
开发语言·javascript·ecmascript
AI360labs_atyun2 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
宇钶宇夕2 小时前
S7-1200 系列 PLC 中 SCL 语言的 PEEK 和 POKE 指令使用详解
运维·服务器·数据库·程序人生·自动化
绿蚁新亭2 小时前
Spring的事务控制——学习历程
数据库·学习·spring
凤年徐3 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
nbsaas-boot3 小时前
多租户架构下的多线程处理实践指南
java·开发语言·spring