QT5.12.12通过ODBC连接到GBase 8s数据库(CentOS)
本示例使用的环境如下:
- 硬件平台:x86_64(amd64)
- 操作系统:CentOS 7.8 2003
- 数据库版本(含CSDK):GBase 8s V8.8 3.0.0_1
为什么使用QT 5.12.10? - 该版本包含QODBC。
1,GBase 8s数据库的ODBC安装与配置
1.1,确认操作系统已经安装unixODBC及unixODBC-devel
通过apt查找确认unixODBC及unixODBC-devel已经安装
1 2 3 | [root@localhost ~]# rpm -qa unixODBC unixODBC-devel unixODBC-devel-2.3.1-14.el7.x86_64 unixODBC-2.3.1-14.el7.x86_64 |
如果未安装使用apt源进行安装
1 | [root@localhost ~]# yum -y install unixODBC unixODBC-devel |
1.2,安装CSDK及配置ODBC
GBase 8s数据库的ODBC驱动程序包含在CSDK组件中。
1.2.1,安装CSDK
解压缩CSDK软件包
1 2 3 4 5 6 7 8 9 10 11 | [root@localhost ~]# mkdir csdk [root@localhost ~]# cd csdk/ [root@localhost csdk]# tar -xvf ../ clientsdk_3.0.0_1_93e040_RHLE6_x86_64.tar csdk.properties doc/ doc/Glsapi_machine_notes_4.10.txt doc/Libcpp_machine_notes_4.10.txt doc/ESQLC_machine_notes_4.10.txt doc/Odbc_machine_notes_4.10.txt installclientsdk .gbase.properties |
执行静默安装,自动完成安装
1 2 | [root@localhost csdk]# ./installclientsdk -i silent \ -DUSER_INSTALL_DIR=/opt/gbase -DLICENSE_ACCEPTED=TRUE |
序号 | 参数名称 | 示例参数值 | 说明信息 |
---|---|---|---|
1 | -i | silent | 指定使用静默安装 |
2 | -DUSER_INSTALL_DIR= | /opt/gbase | 指定安装目录 |
3 | -DLICENSE_ACCEPTED= | TRUE | 指定接受协议 |
配置SQLHOSTS文件内容,在/opt/gbase/etc/sqlhosts配置文件中增加实际数据库地址
1 | gbase01 onsoctcp h01.gbasedbt.com 9088 |
1.2.2,ODBC配置
在/etc/profile配置文件里增加CSDK的配置环境
1 2 3 4 5 6 7 8 9 10 11 12 | # /etc/profile # Add for GBase 8s ODBC export GBASEDBTDIR=/opt/gbase export PATH=${GBASEDBTDIR}/bin:${PATH} export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH export GBASEDBTSQLHOSTS=/opt/gbase/etc/sqlhosts export DB_LOCALE=zh_CN.utf8 export CLIENT_LOCALE=zh_CN.utf8 export GL_USEGLU=1 export ODBCINI=/etc/odbc.ini |
配置/etc/odbcinst.ini配置文件,根据CSDK环境,配置如下:
1 2 3 4 5 6 7 8 9 10 11 | ; /etc/odbcinst.ini ; ODBC Driver for GBase 8s [GBase ODBC DRIVER] Driver=/opt/gbase/lib/cli/iclit09b.so Setup=/opt/gbase/lib/cli/iclit09b.so APILevel=1 ConnectFunctions=YYY DriverODBCVer=03.51 FileUsage=0 SQLLevel=1 smProcessPerConnect=Y |
配置ODBCINI配置文件,根据CSDK环境,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [ODBC Data Sources] testdb=GBase ODBC DRIVER ; ; Define ODBC Database Driver's Below - Driver Configuration Section ; [testdb] ;Driver=/opt/gbase/lib/cli/iclit09b.so Driver=GBase ODBC DRIVER Description=GBase ODBC DRIVER Database=testdb LogonID=gbasedbt pwd=GBase123 Servername=gbase01 CursorBehavior=0 CLIENT_LOCALE=zh_CN.utf8 DB_LOCALE=zh_CN.utf8 GL_USEGLU=1 TRANSLATIONDLL=/opt/gbase/lib/esql/igo4a304.so ; ISOLATIONLEVEL=1 # 使用该参数(简写:ISOLVL)设置默认的隔离级别,0-5 ; ; UNICODE connection Section ; [ODBC] ;uncomment the below line for UNICODE connection UNICODE=UCS-2 # 如果需要使用unicode连接数据库,这里需要去除注释,值改为UCS-2 ; ; Trace file Section ; Trace=0 TraceFile=/tmp/odbctrace.out InstallDir=/opt/gbase TRACEDLL=idmrs09a.so |
ODBC连接测试
确认当前用户环境变量中包括2)中配置的环境变量,3)已经完成。通过isql测试ODBC配置正确。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | [root@localhost ~]# env | egrep '(GBASEDBT|ODBCINI)' GBASEDBTSERVER=gbase01 ODBCINI=/etc/odbc.ini GBASEDBTDIR=/opt/gbase root@netsky:~# isql -v testdb +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> select dbservername from dual; +---------------------------------------+ | | +---------------------------------------+ | gbase01 | +---------------------------------------+ SQLRowCount returns -1 1 rows fetched > root@netsky:~# iusql -v testdb +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> select dbservername from dual; +---------------------------------------+ | | +---------------------------------------+ | gbase01 | +---------------------------------------+ SQLRowCount returns -1 1 rows fetched |
2,QT在Linux(CentOS)的安装与配置
2.1,确认操作系统已经安装gcc和g++
通过apt查找确认gcc、g++、clang已经安装
1 2 3 4 5 6 | [root@localhost ~]# rpm -qa gcc libgcc gcc-c++ clang clang-devel clang-devel-3.4.2-8.el7.x86_64 gcc-4.8.5-44.el7.x86_64 clang-3.4.2-8.el7.x86_64 gcc-c++-4.8.5-44.el7.x86_64 libgcc-4.8.5-44.el7.x86_64 |
如果未安装使用apt源进行安装
1 | [root@localhost ~]# yum -y install gcc libgcc gcc-c++ clang clang-devel |
2.2,安装QT
这里使用5.12.12版本(安装包名称为qt-opensource-linux-x64-5.12.12.run),需要使用到图形化界面
配置环境变量.bash_profile
1 2 3 | # .bash_profile export QTPATH=/opt/Qt5.12.12 export PATH=${QTPATH}/5.12.12/gcc_64/bin:${QTPATH}/Tools/QtCreator/bin:$PATH |
执行安装
1 2 3 | [root@localhost ~]# ./qt-opensource-linux-x64-5.12.12.run [2] Warning: Qt Account settings file [/root/.local/share/Qt/qtaccount.ini] does not exist. [2] Warning: Could not find Qt Account. |
开始安装,要求有Qt的账号,如果没有账号需要注册(如果想跳过该步,需要断网)
接受协议
指定安装目录
选择安装组件,Qt 5.12.12中仅选择Desktop gcc 64-bit
安装过程
完成安装
在 帮助->关于插件中,关闭ClangCodeModel,该操作需要重启才生效
2.3,确认或编译QODBC驱动
查看Qt安装目录下的/opt/Qt5.12.12/5.12.12/gcc_64/plugins/sqldrivers
1 2 3 4 | [root@localhost sqldrivers]# pwd /opt/Qt5.12.12/5.12.12/gcc_64/plugins/sqldrivers [root@localhost sqldrivers]# ls -al *odbc.so -rwxr-xr-x. 1 root root 119352 11月 17 2021 libqsqlodbc.so |
3,QT创建项目通过ODBC连接到GBase 8s数据库
创建新项目
创建Console Application
名称为QtOdbcGBasedbt
使用qmake
使用Desktop Qt 5.12.12 GCC 64bit
在QtOdbcGBasedbt.pro中增加一行QT += sql
在main.cpp中写入代码
main.cpp的详细代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <QCoreApplication> #include <QtSql> #include <QSqlDatabase> int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase( "QODBC" ); // 使用DSN方式 db.setDatabaseName( "testdb" ); //DSN名称 // 使用DSN-Less连接方式 //db.setDatabaseName("DRIVER=/opt/gbase/lib/cli/iclit09b.so;HOST=h01.gbasedbt.com;SERV=9088;PROT=onsoctcp;DLOC=zh_CN.57372;CLOC=zh_CN.57372;DB=testdb;SRVR=gbase01"); db.setUserName( "gbasedbt" ); //用户名 db.setPassword( "GBase123$%" ); //密码 bool ok = db.open(); //建立数据库连接 if (!ok){ qDebug()<< "Can not connect to GBase 8s Database !" ; return -1; } qDebug()<< "Connect to GBase 8s Database OK ." ; QSqlQuery query(db); query.prepare( "drop table if exists tab1" ); bool drop = query.exec(); if (!drop){ qDebug()<< "drop table error !" ; } qDebug()<< "drop table tab1 success ." ; query.prepare( "create table tab1 (id varchar(255), ts datetime year to second default current year to second)" ); bool create = query.exec(); if (!create){ qDebug()<< "create table tab1 error !" ; } qDebug()<< "create table tab1 success ." ; query.prepare( "insert into tab1(id) values('test001')" ); bool insert = query.exec(); if (!insert){ qDebug()<< "insert table tab1 error !" ; } qDebug()<< "insert table tab1 success ." ; query.prepare( "select * from tab1 where id = :id" ); query.bindValue( ":id" , "test001" ); bool select = query.exec(); if (!select){ qDebug()<< "select table tab1 error !" ; } QSqlRecord rec = query.record(); while (query.next()) { rec = query.record(); QString value1 = query.value(0).toString(); QString value2 = query.value(1).toString(); qDebug()<< "id:" <<value1<< "\t" << "ts:" <<value2; } db.close(); return ok; } |
执行debug编译