Qt适配瀚高数据库(Linux)
1.环境准备
1.1.下载安装Qt Creator
- 官网下载链接:http://download.qt.io/archive/qt/
- 执行./qt-opensource-linux-x64-5.13.2.run命令安装,安装需断网,跳过注册
1.2.安装瀚高数据库
安装瀚高安全版4.5.7
2.Qt通过libpq连接HGDB
2.1.导入项目QtConnHgdbByLibpq
打开Qt Creator,选择文件->打开文件或项目,选择项目QtConnHgdbByLibpq存放路径下的QtConnHgdbByLibpq.pro打开。
2.2.编辑QtConnHgdbByLibpq.pro文件,设置include路径和静态链接库
2.3.编辑mainwindow.cpp文件,更改数据库连接信息
2.4.配置项目构建所需环境变量
LD_LIBRARY_PATH=/opt/HighGo4.5.7-see/lib
2.5.运行测试
3.Qt通过ODBC连接HGDB
3.1.修改odbcinst.ini
# 路径在/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/etc/下
[HGDB]
Description=HGDB driver for Linux
Driver=/opt/HighGo4.5.7-see/etc/drivers/ODBC/psqlODBC/lib/psqlodbcw.so
Setup=/opt/HighGo4.5.7-see/etc/drivers/ODBC/psqlODBC/lib/psqlodbcw.so
UsageCount=1
3.2.修改odbc.ini
# 路径在/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/etc/下
[HGDB]
Description=see_for_odbc
Driver=HGDB
Trace=Yes
TraceFile=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/sql.log
Database=highgo
Servername=192.168.248.156
UserName=sysdba
Password=Qwer@1234
Port=5866
Protocol=6.4
ReadOnly=No
RowVersioning=No
ShowSystemTables=No
ShowOidColumn=No
FakeOidIndex=No
3.3.测试连接
cd /opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/bin
# 设置环境变量
export LD_LIBRARY_PATH=/opt/HighGo4.5.7-see/lib:/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/lib:$LD_LIBRARY_PATH
export ODBCSYSINI=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/etc
# 执行测试
./isql -v HGDB
3.4.Qt导入项目QtConnHgdbByODBC
3.5.编辑main.cpp文件,更改数据库连接信息
3.6.配置项目环境变量
LD_LIBRARY_PATH=/opt/HighGo4.5.7-see/lib:/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/lib:$LD_LIBRARY_PATH
ODBCSYSINI=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/etc
3.7.运行项目测试数据库连接
4.问题处理
4.1系统缺少库
#/bin/sh: g++: 未找到命令
yum install gcc gcc-c++
#sh: clang++: 未找到命令
yum install clang clang-c++
#/usr/bin/ld: cannot find -lGL
yum install mesa-libGL-devel mesa-libGLU-devel freeglut-devel
#升级gcc版本,centos7 默认gcc 版本是4.8,与高版本都不能很好的兼容
yum install centos-release-scl
yum install devtoolset-8-gcc*
#新版gcc永久生效
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++
4.2ODBC连接,Qt缺少驱动
4.2.1.如3.7所示,测试连接成功,但低版本的Qt在安装完成后,没有QODBC驱动,需要进行手动编译,以5.12.0版本为例。在运行测试时会报下图错误。
4.2.2.查看Qt驱动
4.2.3.编译ODBC
#进入Qt安装目录下的odbc
cd /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/odbc/
#生成make文件
/opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake "INCLUDEPATH+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/include" "LIBS+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/lib/libodbc.so"
#执行报错,提示找不到/opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/qtsqldrivers-config.pri
# 以下两种方式任选其一
#1. 复制上级目录中的的qsqldriverbase.pri为qtsqldrivers-config.pri
cp ../qsqldriverbase.pri ../qtsqldrivers-config.pri
#2. 将上级目录中的qsqldriverbase.pri文件中的include定义改为qsqldriverbase.pri
vim ../qsqldriverbase.pri
# 修改后再次执行命令
/opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake "INCLUDEPATH+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/include" "LIBS+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/lib/libodbc.so"
# 提示Project ERROR: Library 'odbc' is not defined.
# 修改当前目录中odbc.pro将QMAKE_USE += odbc 前加# 注释掉
vim odbc.pro
# 注释掉再次执行
/opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake "INCLUDEPATH+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/include" "LIBS+=/opt/HighGo4.5.7-see/etc/drivers/ODBC/unixODBC/lib/libodbc.so"
# 执行完成生成make文件
#执行
make && make install
#执行完成后到/opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/下查看odbc驱动已经生成
cd /opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/
4.2.4.重新运行项目
4.3.QPSQL连接,缺少驱动
4.3.1查看驱动
#进入qt sqldrivers目录
cd /opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/
4.3.2.编译psql
#进入Qt安装目录下的psql
cd /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/psql
#生成make文件
/opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake "INCLUDEPATH+=/opt/HighGo4.5.7-see/include" "LIBS+=/opt/HighGo4.5.7-see/lib/libpq.so.5"
# 提示Project ERROR: Library 'psql' is not defined.
# 修改当前目录中psql.pro将QMAKE_USE += psql 前加# 注释掉
# 注释掉再次执行
/opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake "INCLUDEPATH+=/opt/HighGo4.5.7-see/include" "LIBS+=/opt/HighGo4.5.7-see/lib/libpq.so.5"
# 执行完成生成make文件
#执行
make && make install
#执行完成后到/opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/下查看psql驱动已经生成
cd /opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/