在下载网址,下载相应版本qt
https://download.qt.io/archive/qt/
安装依赖并开始安装
[root@localhost opt]# sudo yum install flex bison gperf
[root@localhost opt]# sudo yum install libxkbcommon-x11
[root@localhost opt]# ./qt-opensource-linux-x64-5.12.8.run
注:注册这一步可能会让你邮箱验证
注意:执行安装包可能报以下错误,是缺少依赖导致
[root@localhost opt]# ./qt-opensource-linux-x64-5.12.8.run
./qt-opensource-linux-x64-5.12.8.run: error while loading shared libraries: libxkbcommon-x11.so.0: cannot open shared object file: No such file or directory
(1)安装配置unixODBC
http://www.unixodbc.org(文件下载地址)
[root@localhost opt]# tar -zxvf unixODBC-2.3.11.tar.gz
[root@localhost unixODBC-2.3.11]# ./configure
[root@localhost unixODBC-2.3.11]# make && make install
[root@localhost ~]# vim /usr/local/etc/odbcinst.ini
[DM8 ODBC DRIVER]
Description = ODBC DRIVER FOR DM8
Driver = /opt/dmdbms/bin/libdodbc.so
[root@localhost ~]# vim /usr/local/etc/odbc.ini
[DM8]
Description = DM ODBC DSN
Driver = DM8 ODBC DRIVER
SERVER = localhost
UID = SYSDBA
PWD = SYSDBA
TCP_PORT = 5236
[root@localhost opt]# odbcinst -q -s
[DM8]
[root@localhost opt]# isql -v DM8 SYSDBA SYSDBA
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| echo [string] |
| quit |
| |
+---------------------------------------+
SQL>
(2)首先进入qt源码目录中修改qsqldriverbase.pri文件,将include部分修改为下图所示内容
[root@localhost sqldrivers]# cd /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers
[root@localhost sqldrivers]# vim qsqldriverbase.pri
include($$shadowed($$PWD)../sqldrivers/configure.pri)
(3)修改qt源码目录中的odbc.pro文件,注释QMAKE_USE += odbc
[root@localhost sqldrivers]# cd /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/odbc
[root@localhost odbc]# vim odbc.pro
(4)在opt/Qt5.12.8/5.12.8/gcc_64/bin目录下进行编译
[root@localhost include]# cd /opt/Qt5.12.8/5.12.8/gcc_64/bin
[root@localhost bin]# odbc_config --libs
-L/usr/local/lib -lodbc
[root@localhost bin]# odbc_config --include-prefix
/usr/local/include
[root@localhost bin]# ./qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lodbc" /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/odbc/odbc.pro
[root@localhost sqldrivers]# find / -name libqsqlodbc.so.debug
/plugins/sqldrivers/libqsqlodbc.so.debug
[root@localhost sqldrivers]# cd /plugins/sqldrivers/
[root@localhost sqldrivers]# ll
总用量 1244
-rwxr-xr-x 1 root root 123736 12月 6 16:36 libqsqlodbc.so
-rw-r--r-- 1 root root 1145064 12月 6 16:36 libqsqlodbc.so.debug
在bin目录下进行编译会在该目录下生成makefile文件,执行qmake即可生成libqsqlodbc.so,将此动态库复制到qt目录下的sqldrivers目录下即可正常使用qodbc接口对达梦进行操作;(注:一般下载qt会自带libqsqlodbc.so,如果没有请安装上述操作)
注意:如果报没有环境变量g++,使用g++ --version命令产看,如果未找到命令需要安装g++
[root@localhost bin]# ./qmake "INCLUDEPATH+=/opt/unixODBC-2.3.11/include" "LIBS+=-L/opt/unixODBC-2.3.11/lib -lodbc" /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/odbc/odbc.pro
Project ERROR: Cannot run compiler 'g++'. Output:
===================
===================
Maybe you forgot to setup the environment?
[root@localhost bin]# g++ --version
-bash: g++:未找到命令
[root@localhost bin]# sudo yum install gcc-c++
[root@localhost bin]# g++ --version
g++ (GCC) 7.3.0
Copyright © 2017 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[root@localhost bin]#
注意:如果报没有config.pri文件,进到该目录下将configure.pri复制一份并更名为config.pri文件
[root@localhost bin]# ./qmake "INCLUDEPATH+=/opt/unixODBC-2.3.11/include" "LIBS+=-L/opt/unixODBC-2.3.11/lib-lodbc" /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/odbc/odbc.pro
Info: creating stash file /opt/Qt5.12.8/5.12.8/gcc_64/.qmake.stash
Cannot read /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/config.pri: No such file or directory
[root@localhost sqldrivers]# cp configure.pri config.pri
注意:如果qmake 报未定义请查看odbc_config中lib与include,确认qmake路径正确
[root@localhost bin]# odbc_config --libs
-L/usr/local/lib -lodbc
[root@localhost bin]# odbc_config --include-prefix
/usr/local/include
(1)右击文件新建文件或项目
(2)填写名称与路径
(3)选择编译方式为qmake
(4)查看类信息
(5)语言选中文
(6)位数选择
(7)查看项目信息
(8)在新建项目中修改为如下
#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QTextCodec>
#include <QApplication>
#include<QtPlugin>
//Q_IMPORT_PLUGIN(QDMDriverPlugin)
int main(int argc, char *argv[])
//显示可用qt驱动
{
QCoreApplication a(argc, argv);
QString strLibPath(QDir::toNativeSeparators(QApplication::applicationDirPath()) + QDir::separator() + "sqldrivers");
a.addLibraryPath(strLibPath);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString dvr,drivers)
{
qDebug()<<dvr;
}
//定义数据库添加驱动
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setPort(5236);
db.setDatabaseName("DM8");
db.setUserName("SYSDBA");
db.setPassword("SYSDBA");
if (db.open())
qDebug() << "connect ok!";
else
{
qDebug() << "connect fail! " << db.lastError().text().toLatin1();
}
//定义查询
QSqlQuery query;
//清空表,初始化测试环境,测试绑定变量插入数据
QString strsql = "drop table if exists test01";
query.exec(strsql);
strsql = "create table test01(name varchar2(10),id int default 1)";
query.exec(strsql);
strsql = "insert into test01(name) values(?)";
query.prepare(strsql);
query.bindValue(0, "物理");
if (query.exec())
{
qDebug() << "insert ok!";
}
else
{
qDebug() << "insert fail! " << query.lastError().text();
getchar();
exit(-1);
}
//查询数据
strsql = "select * from test01";
if (query.exec(strsql))
{
qDebug() << "select ok!";
}
else
{
qDebug() << "select fail! " << query.lastError().text();
getchar();
exit(-1);
}
while (query.next())
{
qDebug() << query.value(0).toString().toStdString().c_str()<<"," << query.value(1).toInt();
}
query.clear();
return a.exec();
}
注意:如果代码出现QTSQL未找到,请在左边XXX.pro文件中添加QT += sql保存即可
注意:如果编译过程中出现cannot find -lGL,请安装或者查找libGL.so.1文件移到对应目录
[root@localhost bin]# yum install mesa-libGL-devel mesa-libGLU-devel
注意:如果报找不到文件,可能未环境变量未配置好重新检查
(1)下载源码
登陆https://eco.dameng.com/download/下载
(2)解压并拷贝源码
拷贝 qt_src\plugins_sqldrivers\dm_56 到 $QTDIR/Src/qtbase/src/plugins/sqldrivers,并改名为 dm;
拷贝 qt_src\sql_drivers\dm 到 $QTDIR/Src/qtbase/src/sql/drivers 目录,没有这里的drivers目录的话可以自行创建。
[root@localhost opt]# unzip qt_src-766-20211228.zip
[root@localhost ~]# cd /opt/qt_src/plugins_sqldrivers
[root@localhost plugins_sqldrivers]# cp -r dm_56/ /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/dm
[root@localhost ~]# cd /opt/qt_src/sql_drivers
[root@localhost sql_drivers]# cp -r dm/ /opt/Qt5.12.8/5.12.8/Src/qtbase/src/sql/drivers/
(3)进入 $QTDIR/gcc_64/bin 目录
执行qmake后该目录下会生成makefile,执行make debug会生成libqsqldm.so,后将其放在 $QTDIR/gcc_64/plugins/sqldrivers/ 目录,即可正常使用QDM接口对达梦进行操作;
[root@localhost ~]# cd /opt/Qt5.12.8/5.12.8/gcc_64/bin
[root@localhost bin]# ./qmake "INCLUDEPATH+=/opt/dmdbms/include" "LIBS+=/opt/dmdbms/include/libdmdpi.a -ldl -lrt" "DEFINES+=DM64" "CONFIG+=debug_and_release" /opt/Qt5.12.8/5.12.8/Src/qtbase/src/plugins/sqldrivers/dm/dm.pro
[root@localhost bin]# make debug
[root@localhost bin]# find / -name libqsqldm.so.debug
/plugins/sqldrivers/libqsqldm.so.debug
[root@localhost sqldrivers]# cp /plugins/sqldrivers/libqsqldm.so /opt/Qt5.12.8/5.12.8/gcc_64/plugins/sqldrivers
创建项目与QODBC过程一致
#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QTextCodec>
#include <QApplication>
#include<QtPlugin>
//Q_IMPORT_PLUGIN(QDMDriverPlugin)
int main(int argc, char *argv[])
//显示可用qt驱动
{
QCoreApplication a(argc, argv);
QString strLibPath(QDir::toNativeSeparators(QApplication::applicationDirPath()) + QDir::separator() + "sqldrivers");
a.addLibraryPath(strLibPath);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString dvr,drivers)
{
qDebug()<<dvr;
}
//定义数据库添加驱动
QSqlDatabase db = QSqlDatabase::addDatabase("QDM");
db.setHostName("localhost");
db.setPort(5236);
db.setDatabaseName("DM8");
db.setUserName("SYSDBA");
db.setPassword("SYSDBA");
if (db.open())
qDebug() << "connect ok!";
else
{
qDebug() << "connect fail! " << db.lastError().text().toLatin1();
}
//定义查询
QSqlQuery query;
//清空表,初始化测试环境,测试绑定变量插入数据
QString strsql = "drop table if exists test01";
query.exec(strsql);
strsql = "create table test01(name varchar2(10),id int default 1)";
query.exec(strsql);
strsql = "insert into test01(name) values(?)";
query.prepare(strsql);
query.bindValue(0, "物理");
if (query.exec())
{
qDebug() << "insert ok!";
}
else
{
qDebug() << "insert fail! " << query.lastError().text();
getchar();
exit(-1);
}
//查询数据
strsql = "select * from test01";
if (query.exec(strsql))
{
qDebug() << "select ok!";
}
else
{
qDebug() << "select fail! " << query.lastError().text();
getchar();
exit(-1);
}
while (query.next())
{
qDebug() << query.value(0).toString().toStdString().c_str()<<"," << query.value(1).toInt();
}
query.clear();
return a.exec();
}
注意:如果报加密模块加载失败,把数据库中libcrypto.so文件传到/usr/lib64/目录下
[root@localhost lib64]# cp /opt/dmdbms/bin/libcrypto.so /usr/lib64/
文章
阅读量
获赞