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 分钟前
SQL 复杂查询
数据库·sql
Alive~o.07 分钟前
Go语言进阶&依赖管理
开发语言·后端·golang
斗-匕7 分钟前
MySQL 三大日志详解
数据库·mysql·oracle
花海少爷9 分钟前
第十章 JavaScript的应用课后习题
开发语言·javascript·ecmascript
手握风云-10 分钟前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构
代码中の快捷键13 分钟前
MySQL数据库存储引擎
数据库·mysql
只因在人海中多看了你一眼14 分钟前
数据库体系
数据库
喵叔哟30 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生36 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
六月闻君1 小时前
MySQL 报错:1137 - Can‘t reopen table
数据库·mysql