Qt6编译达梦8数据库驱动插件
一、编译环境
操作系统:deepin V23
Qt版本: Qt 6.7.2
编译器:gcc/g++ version 12.3.0,cmake 3.28.3
达梦数据库:开发版V8
二、下载达梦QT接口源码
下载链接:
https://eco.dameng.com/download/?_blank
下载Qt编译qt_src-1113-20240428.zip,将其中dm_57中源代码解压至
${QTHOME}/6.7.2/Src/qtbase/src/plugins/sqldrivers/dm 目录下
三、修改Qt编译相关配置文件
1. CMakeLists.txt
${QTHOME}/6.7.2/Src/qtbase/src/plugins/sqldrivers/CMakeLists.txt
#### if(QT_FEATURE_sql_db2) 行前插入
if(QT_FEATURE_sql_dm)
add_subdirectory(dm)
endif()
2. configure.cmake
${QTHOME}/6.7.2/Src/qtbase/src/plugins/sqldrivers/configure.cmake
#### Libraries 部分 qt_find_package(ODBC 行前插入
qt_find_package(DM PROVIDED_TARGETS DM::DM MODULE_NAME sqldrivers QMAKE_LIB dm)
#### Features 部分 qt_feature("sql-odbc" 行前插入
qt_feature("sql-dm" PRIVATE
LABEL "DM (DaMeng)"
CONDITION DM_FOUND
)
#### 最后一段中qt_configure_add_summary_entry(ARGS "sql-odbc")行前插入
qt_configure_add_summary_entry(ARGS "sql-dm")
3.qt_cmdline.cmake
${QTHOME}/6.7.2/Src/qtbase/src/plugins/sqldrivers/qt_cmdline.cmake
#### qt_commandline_option(sql-odbc TYPE boolean)行前插入
qt_commandline_option(sql-dm TYPE boolean)
#### qt_commandline_option(plugin-sql-odbc TYPE void NAME sql-odbc)行前插入
qt_commandline_option(plugin-sql-dm TYPE void NAME sql-dm)
4.编写FindDM.cmake文件
${QTHOME}/6.7.2/gcc_64/lib/cmake/Qt6/FindDM.cmake
5.编写DMConfig.cmake文件
${DM_DIR}/DMConfig.cmake
四、配置环境变量
加入~/.bashrc:
export QTHOME=/home/${USER}/Qt
export QTVER=6.7.2
export QTDIR=$QTHOME/$QTVER
export PATH=$PATH:$QTDIR/gcc_64/bin:$QTHOME/Tools/CMake/bin
export LD_LIBRARY_PATH=$QTDIR/gcc_64/lib:$LD_LIBRARY_PATH
export QTBASE=$QTDIR/Src/qtbase
export DM_DIR=/home/${USER}/dmdbms
export LD_LIBRARY_PATH=$QM_DIR/drivers/dpi:$LD_LIBRARY_PATH
五、创建编译目录
~$ mkdir ~/build-qdm && cd ~/build-qdm
六、生成CMAKE编译文件
$ qt-cmake -G Ninja $QTBASE/src/plugins/sqldrivers \
-DDM_INCLUDE_DIR="$DM_DIR/include" \
-DDM_LIBRARY="$DM_DIR/drivers/dpi/libdmdpi.so" \
-DCMAKE_MAKE_PROGRAM="$QTHOME/Tools/Ninja/ninja"
七、编译构建
生成的oci驱动插件包目录为:$BUILD-DM/plugins/sqldrivers
$ cmake --build .
八、安装
安装路径为 $CMAKE_INSTALL_PREFIX/plugins/sqldrivers
$ cmake --install .
九、Qt测试代码:
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QDM");
db.setHostName("localhost");
db.setDatabaseName("DMSERVER");
db.setPort(5236);
db.setUserName("SYSDBA");
db.setPassword("SYSDBA");
bool ret=db.open();
qDebug()<<"db open is "<<ret;
if (!query.exec(sql))
{
qDebug()<<query.lastError();
return false;
}
db.close();
a.exit();
}