Qt5.4.1连接odbc驱动操作达梦数据库
1 环境介绍
- CPU : x86/c86
- 操作系统 : 麒麟V10 (SP2)
- qt下载地址 - - 点击跳转
- ODBC版本 : unixODBC 2.3.7
- 达梦数据库版本 : 1-3-162-2024.07.03-234060-20108-ENT
2 Qt5.4.1 安装
2.1 图形化界面安装Qt5.4.1
- 将安装包上传到 /opt 目录 使用root安装
chmod +x qt-opensource-linux-x64-5.4.1.run
./qt-opensource-linux-x64-5.4.1.run
2.2 配置Qt5.4.1 环境变量
- 末尾 4行 是 Qt 环境变量配置
vi /root/.bash_profile
[root@localhost opt]# cat /root/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/dmdba/dmdbms2407/bin"
export DM_HOME="/home/dmdba/dmdbms2407"
export QTDIR=/opt/Qt5.4.1/5.4/gcc_64
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/bin:$QTDIR/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=$QTDIR/lib/pkgconfig:$PKG_CONFIG_PATH
[root@localhost opt]#
- 验证环境变量是否生效
[root@localhost test1]# echo $QTDIR
/opt/Qt5.4.1/5.4/gcc_64
[root@localhost test1]# echo $LD_LIBRARY_PATH
/opt/Qt5.4.1/5.4/gcc_64/bin:/opt/Qt5.4.1/5.4/gcc_64/lib:/opt/Qt5.4.1/5.4/gcc_64/lib:/opt/Qt5.4.1/5.4/gcc_64/lib::/home/dmdba/dmdbms2407/bin:/home/dmdba/dmdbms2407/bin:/home/dmdba/dmdbms2407/bin:/home/dmdba/dmdbms2407/bin:/home/dmdba/dmdbms2407/bin
[root@localhost test1]#
- 查询 Qt5.4.1 版本
[root@localhost test1]# qmake --version
QMake version 3.0
Using Qt version 5.4.1 in /opt/Qt5.4.1/5.4/gcc_64/lib
[root@localhost test1]#
2.3 Qt5.4.1 生成 libqsqlodbc.so 并配置
- 实际环境可能会碰到各种报错 , 一言难尽
- libqsqlodbc.so - - 点击下载
2.3.1 生成Makefile
- 逐行执行
cd /opt/Qt5.4.1/5.4/gcc_64/bin
./qmake "LIBS+=-L/usr/lib64 -lodbc" /opt/Qt5.4.1/5.4/Src/qtbase/src/plugins/sqldrivers/odbc/odbc.pro
make
- 结束时末尾日志 :
../gcc_64/include/QtCore/qvector.h:502:1: ????????????defined???????????????? [-Wexpansion-to-defined]
#if QT_SUPPORTS(UNSHARABLE_CONTAINERS)
^~~~~~~~~~~~~~~~~~~~~~~~~~~
../gcc_64/include/QtCore/qvector.h:570:1: ????????????defined???????????????? [-Wexpansion-to-defined]
#if QT_SUPPORTS(UNSHARABLE_CONTAINERS)
^~~~~~~~~~~~~~~~~~~~~~~~~~~
rm -f libqsqlodbc.so
g++ -Wl,--no-undefined -Wl,-O1 -Wl,-rpath,/opt/Qt5.4.1/5.4/gcc_64 -Wl,-rpath,/opt/Qt5.4.1/5.4/gcc_64 -Wl,-rpath,/opt/Qt5.4.1/5.4/gcc_64/lib -shared -o libqsqlodbc.so .obj/main.o .obj/qsql_odbc.o .obj/moc_qsql_odbc_p.o -L/usr/lib64 -lodbc -L/opt/Qt5.4.1/5.4/gcc_64/lib -lQt5Sql -lQt5Core -lpthread
mv -f libqsqlodbc.so /plugins/sqldrivers/
[root@localhost bin]#
2.3.2 查看 libqsqlodbc.so 文件并配置
[root@localhost bin]# ls /plugins/sqldrivers/
libqsqlodbc.so
[root@localhost bin]#
[root@localhost bin]# cp /plugins/sqldrivers/libqsqlodbc.so /opt/Qt5.4.1/5.4/gcc_64/plugins/sqldrivers/
[root@localhost bin]#
[root@localhost bin]# ls /opt/Qt5.4.1/5.4/gcc_64/plugins/sqldrivers/
libqsqlite.so libqsqlmysql.so libqsqlodbc.so libqsqlpsql.so
[root@localhost bin]#
3 配置Qt测试用例
qmake "LIBS+=-L/usr/lib64 -lodbc" /opt/Qt5.4.1/5.4/test1/test1.pro
make
./test1
- 末尾日志
[root@localhost test1]# qmake "LIBS+=-L/usr/lib64 -lodbc" /opt/Qt5.4.1/5.4/test1/test1.pro
[root@localhost test1]# ls
bak release test1.pro.user.c628fdd
debug main.o test1.pro test1_zh_CN.ts
main.cpp Makefile test1.pro.user
[root@localhost test1]# make
g++ -c -pipe -O2 -std=c++0x -Wall -W -D_REENTRANT -fPIE -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_CORE_LIB -I. -I../gcc_64/include -I../gcc_64/include/QtSql -I../gcc_64/include/QtCore -I. -I../gcc_64/mkspecs/linux-g++ -o main.o main.cpp
In file included from ../gcc_64/include/QtCore/qbytearray.h:37:0,
from ../gcc_64/include/QtCore/qstring.h:38,
from ../gcc_64/include/QtCore/qcoreapplication.h:38,
from ../gcc_64/include/QtCore/QCoreApplication:1,
from main.cpp:1:
../gcc_64/include/QtCore/qrefcount.h:50:1: ????????????defined???????????????? [-Wexpansion-to-defined]
...中间省略...
...中间省略...
...中间省略...
../gcc_64/in
clude/QtCore/qalgorithms.h:148:40: ??????????????
class QT_DEPRECATED_X("Use std::less") qLess
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt5.4.1/5.4/gcc_64 -Wl,-rpath,/opt/Qt5.4.1/5.4/gcc_64/lib -o test1 main.o -L/usr/lib64 -lodbc -L/opt/Qt5.4.1/5.4/gcc_64/lib -lQt5Sql -lQt5Core -lpthread
[root@localhost test1]# ls
bak main.cpp.240906.bak release test1.pro.user
debug main.o test1 test1.pro.user.c628fdd
main.cpp Makefile test1.pro test1_zh_CN.ts
[root@localhost test1]# ./test1
connect ok!
select ok!
[root@localhost test1]#