网站源码下载网,翻墙在线代理,图片设计网站推荐,文件注入网站示例环境#xff1a;
操作系统#xff1a;Windows 10 64位数据库及CSDK版本#xff1a;GBase 8s V8.8_3.0.0_1 64位QT#xff1a;5.12.0 64位
1#xff0c;CSDK安装及ODBC配置
1.1#xff0c;免安装版CSDK
下载免安装版的CSDK驱动#xff0c;地址#xff1a;https:…示例环境
操作系统Windows 10 64位数据库及CSDK版本GBase 8s V8.8_3.0.0_1 64位QT5.12.0 64位
1CSDK安装及ODBC配置
1.1免安装版CSDK
下载免安装版的CSDK驱动地址https://gbasedbt.com/dl/odbc/GBase8s_3.0.0_1-Win64-ODBC-Driver.zip 解压到不含中文的目录下如D盘根目录下生成gbase8s-odbc-driver目录 使用管理员身份运行 00注册ODBC_管理员权限运行.cmd将自动注册ODBC。
1.2ODBC数据源配置
在ODBC数据库64位中配置数据源DSN 在用户DSN或者系统DSN中创建新数据源使用的驱动名称为GBase ODBC DRIVER(64-bit),名称为testdb 连接属性Connection和环境Environment按实际数据库相应值的进行配置
2QT创建项目通过ODBC连接数据库测试
打开QT Creator新建Qt Console Application项目 名称为QtOdbcGBasedbt,目录指定在D:\WORKDIR下 使用qmake编译系统 Kit Selection使用Desktop Qt 5.12.0 MinGW 64-bit 连接数据库操作需要在QtOdbcGBasedbt.pro配置文件中增加一行QT sql 在main.cpp中编写连接数据库的代码使用ODBC连接可以使用DSN的方式使用setDatabaseName(“ODBC数据源中配置的DSN名称”)或者使用直连DSN-Less方式使用setDatabaseName(“ODBC连接字符串一般包含DRIVER、HOST、SERV、PROT、DB和SRVR等参数”) 示例使用的完整main.cpp内容如下
#include QCoreApplication
#include QtSql
#include QSqlDatabaseint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QSqlDatabase db QSqlDatabase::addDatabase(QODBC);// 使用DSN方式db.setDatabaseName(testdb); //DSN名称// 使用DSN-Less连接方式//db.setDatabaseName(DRIVER{GBase ODBC DRIVER (64-bit)};HOSTh01.gbasedbt.com;SERV9088;PROTonsoctcp;DLOCzh_CN.57372;CLOCzh_CN.57372;DBtestdb;SRVRgbase01);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\tts:value2;}db.close();return ok;
}执行debug测试输出显示操作成功。
附件 QtOdbcGBasedbt.zip