Qt笔记-Qt使用DPI访问达梦7数据库

x33g5p2x  于2022-06-01 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(585)

这里开发环境以Windows为例,官方其实已经有了C/C++使用DPI,这里本人只是记录下重点,方便以后进行查阅。

首先是驱动不能少:

这里我用的编译器是MSVC140,版本把,也就是vs2015

Qt.pro文件:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

INCLUDEPATH += D:/DM/DM/source/drivers/dpi/include
LIBS += -LD:/DM/DM/source/drivers/dpi -ldmdpi

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp

#include <QCoreApplication>
#include <QDebug>
#include "DPI.h"
#include "DPIext.h"
#include "DPItypes.h"

#define DM_SVR "127.0.0.1:5236"
#define DM_USER "SYSDBA"
#define DM_PWD "SYSDBA"

dhenv henv; /* 环境句柄 */
dhcon hcon; /* 连接句柄 */
dhstmt hstmt; /* 语句句柄 */
dhdesc hdesc; /* 描述符句柄 */
DPIRETURN rt; /* 函数返回值 */

void dpi_err_msg_print(sdint2 hndl_type, dhandle hndl)
{
    sdint4 err_code;
    sdint2 msg_len;
    sdbyte err_msg[SDBYTE_MAX];

    /* 获取错误信息集合 */
    dpi_get_diag_rec(hndl_type, hndl, 1, &err_code, err_msg, sizeof(err_msg), &msg_len);
    printf("err_msg = %s, err_code = %d\n", err_msg, err_code);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    sdint4  out_c1 = 0;
    sdbyte  out_c2[20]= { 0 };
    slength out_c1_ind = 0;
    slength out_c2_ind = 0;
    ulength row_num;

    //连接数据库
        /* 申请环境句柄 */
        rt = dpi_alloc_env(&henv);

        /* 申请连接句柄 */
        rt = dpi_alloc_con(henv, &hcon);

        /* 连接数据库服务器 */
        rt = dpi_login(hcon, (sdbyte *)DM_SVR, (sdbyte *)DM_USER, (sdbyte *)DM_PWD);
        if(!DSQL_SUCCEEDED(rt))
        {
            dpi_err_msg_print(DSQL_HANDLE_DBC, hcon);
            return rt;
        }
        printf("dpi: connect to server success!\n");

        rt = dpi_alloc_stmt(hcon, &hstmt);

        //select * from CLCC.CLCC_REGION
        qDebug() << dpi_exec_direct(hstmt, (sdbyte*) "select * from CLCC.CLCC_REGION");
        dpi_bind_col(hstmt, 1, DSQL_C_SLONG, &out_c1, sizeof(out_c1), &out_c1_ind);
        dpi_bind_col(hstmt, 2, DSQL_C_NCHAR, &out_c2, sizeof(out_c2), &out_c2_ind);

        printf("dpi: select * from table...\n");
        while(dpi_fetch(hstmt, &row_num) != DSQL_NO_DATA)
        {
            printf("c1 = %d, c2 = %s ,\n", out_c1, out_c2);
        }
        printf("dpi: select success\n");

        //断开数据库连接
        /* 断开连接 */
        rt = dpi_logout(hcon);
        if(!DSQL_SUCCEEDED(rt))
        {
            dpi_err_msg_print(DSQL_HANDLE_DBC, hcon);
        return rt;
        }
        printf( "dpi: disconnect from server success!\n" );

        /* 释放连接句柄和环境句柄 */
        rt = dpi_free_con(hcon);
        rt = dpi_free_env(henv);

        return rt;

    return a.exec();
}

最简单的实例代码已经上来了,源码打包下载如下:

Qt/DPIWindows at master · fengfanchen/Qt · GitHub

剩下的就是大家自己去封装把,这里我封装的代码不能开源。

相关文章