gbase8s odbc 操作 blob、clob简单示例

gbase8s支持两种操作方式

一种是标准odbcAPI接口,需要设置连接属性SQL_INFX_ATTR_LO_AUTOMATIC 为 true

令一种是gbase提供的内部函数接口

一 使用标准ODBC API 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /*NO_WIN32*/

#include "infxcli.h"


#define BUFFER_LEN   100
#define ERRMSG_LEN   200

SQLINTEGER checkError (SQLRETURN       rc,
                SQLSMALLINT     handleType,
				SQLHANDLE       handle,
				SQLCHAR*            errmsg)
{
    SQLRETURN       retcode = SQL_SUCCESS;

    SQLSMALLINT     errNum = 1;
	SQLCHAR		    sqlState[6];
    SQLINTEGER      nativeError;
	SQLCHAR		    errMsg[ERRMSG_LEN];
    SQLSMALLINT     textLengthPtr;
 

    if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
    {
        while (retcode != SQL_NO_DATA)
        {
            retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);

            if (retcode == SQL_INVALID_HANDLE)
            {
                fprintf (stderr, "checkError function was called with an invalid handle!!\n");
                return 1;
            }

            if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
                fprintf (stderr, "ERROR: %d:  %s : %s \n", nativeError, sqlState, errMsg);

            errNum++;
        }

        fprintf (stderr, "%s\n", errmsg);
        return 1;
    }
	else
		return 0;
}


int main (long         argc,
          char*        argv[])
{        

    SQLCHAR* defDsn = "DRIVER=/home/gbasedbt/install_dir/330_2/lib/cli/iclit09b.so;DLOC=zh_CN.57372;CLOC=zh_CN.57372;DB=test;SRVR=er01;UID=root;PWD=123456";

    SQLHDBC         hdbc;
    SQLHENV         henv;
    SQLHSTMT        droptabstmt;
	SQLHSTMT        createtabstmt;
	SQLHSTMT        hInsertStmt;
	SQLHSTMT        selectStmt;
    SQLRETURN       rc,in = 0;
    //SQLCHAR         connStrIn[200];
    SQLCHAR         connStrOut[200];
    SQLSMALLINT     connStrOutLen;
    SQLCHAR         instr[100];
	
	SQLCHAR         c1[BUFFER_LEN];
	SQLCHAR         c2[BUFFER_LEN];
	SQLLEN          cbc1,cbc2;
	SQLCHAR* droptabsql = (SQLCHAR*)"drop table if exists t1";
	SQLCHAR* createtabsql = "create table t1(c1 blob,c2 clob)";
    SQLCHAR* insertsql = (SQLCHAR*)"insert into t1 values(?,?)";
	
	SQLCHAR* selectsql = (SQLCHAR*)"select c1,c2 from t1;";
    /* Allocate the Environment handle */
    rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (rc != SQL_SUCCESS)
    {
        fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
        return (1);
    }

    /* Set the ODBC version to 3.0 */
    rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
		return (1);


    /* Allocate the connection handle */
    rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
		return (1);


    /* Establish the database connection */
    rc = SQLDriverConnect (hdbc, NULL, (char*)defDsn , SQL_NTS, connStrOut, 200, &connStrOutLen, SQL_DRIVER_NOPROMPT);

	if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
		return (1);

    
	rc = SQLSetConnectAttr(hdbc,SQL_INFX_ATTR_LO_AUTOMATIC,(SQLPOINTER)SQL_TRUE,0);
	if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLSetConnectAttr failed\nExiting!!"))
		return (1);
	
    /* Allocate the statement handle for the CREATE TABLE statement */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &droptabstmt);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Create Table Statement Handle Allocation failed\nExiting!!"))
		return (1);

    /* Allocate the statement handle for the CREATE TABLE statement */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &createtabstmt);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Create Table Statement Handle Allocation failed\nExiting!!"))
		return (1);

    /* Allocate the statement handle for the INSERT statement */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hInsertStmt);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Insert Statement Handle Allocation failed\nExiting!!"))
		return (1);
	
	rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &selectStmt);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Insert Statement Handle Allocation failed\nExiting!!"))
		return (1);
    

	fprintf (stdout, "STEP 1 done...connected to database\n");


    rc = SQLExecDirect (droptabstmt, droptabsql, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, droptabstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n" ))
		goto Exit;

   	rc = SQLExecDirect (createtabstmt, createtabsql, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, createtabstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n" ))
		goto Exit;


    rc = SQLPrepare (hInsertStmt, insertsql, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n" ))
		goto Exit;

    SQLCHAR textData[] = "this";


    rc = SQLBindParameter(hInsertStmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY,BUFFER_LEN, 0,(SQLPOINTER)textData, 0, NULL);
	rc = SQLBindParameter(hInsertStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, BUFFER_LEN, 0,(SQLPOINTER)textData, 0, NULL);

    rc = SQLExecute(hInsertStmt);

    if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n" ))
		goto Exit;
	
	
	rc = SQLExecDirect (selectStmt, selectsql, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
        goto Exit;
	
	 /* Bind the result set columns */
    rc = SQLBindCol (selectStmt, 1, SQL_C_BINARY, c1, BUFFER_LEN, &cbc1);
    if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
        goto Exit;

    rc = SQLBindCol (selectStmt, 2, SQL_C_CHAR, c2, BUFFER_LEN, &cbc2);
    if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
        goto Exit;


     while (1)
    {
        /* Fetch the data */
        rc = SQLFetch (selectStmt);
        if (rc == SQL_NO_DATA_FOUND)
            break;
        else if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
            goto Exit;

        /* Display the results */
        fprintf (stdout, "c1: %s, c2: %s\n", c1,c2);

    }

    /* Close the result set cursor */
    rc = SQLCloseCursor(selectStmt);
    if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
        goto Exit;

    Exit:


    /* Close all the statement handles */
    SQLFreeStmt (droptabstmt, SQL_CLOSE);
    SQLFreeStmt (createtabstmt, SQL_CLOSE);
    SQLFreeStmt (hInsertStmt, SQL_CLOSE);
	SQLFreeStmt (selectStmt, SQL_CLOSE);

    /* Free all the statement handles */
    SQLFreeHandle (SQL_HANDLE_STMT, droptabstmt);
    SQLFreeHandle (SQL_HANDLE_STMT, createtabstmt);
    SQLFreeHandle (SQL_HANDLE_STMT, hInsertStmt);
	SQLFreeHandle (SQL_HANDLE_STMT, selectStmt);


	/* Disconnect from the data source */
    SQLDisconnect (hdbc);

    /* Free the environment handle and the database connection handle */
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
    SQLFreeHandle (SQL_HANDLE_ENV, henv);

    fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
    in = getchar ();
    return (rc);
}



--编译
gcc -g -c -fsigned-char -DNO_WIN32 -O -I/home/gbasedbt/install_dir/330_2/incl/cli -I/home/gbasedbt/install_dir/330_2/incl/esql -I/home/gbasedbt/install_dir/330_2/incl/dmi haha.c
gcc -g -o haha haha.o -L/home/gbasedbt/install_dir/330_2/lib/cli -L/home/gbasedbt/install_dir/330_2/lib/esql -lifcli -lifdmr -L/home/gbasedbt/install_dir/330_2/lib/esql -lifgls -lifglx -lm -lnsl

二 使用gbase8s提供的 ifx_lo等函数

1 插入

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /*NO_WIN32*/
#include <fcntl.h>
#include "infxcli.h"
#define BUFFER_LEN      25
#define ERRMSG_LEN      200


int main (long         argc,
          char*        argv[])
{
    
	SQLCHAR*   defDsn = "DRIVER=/home/gbasedbt/install_dir/330_2/lib/cli/iclit09b.so;DLOC=zh_CN.57372;CLOC=zh_CN.57372;DB=test;SRVR=er01;UID=root;PWD=123456";
    SQLHDBC         hdbc;
    SQLHENV         henv;
    SQLHSTMT        hstmt;

    SQLCHAR         connStrIn[200];
    SQLCHAR         connStrOut[200];
    SQLSMALLINT     connStrOutLen;
    SQLCHAR         dsn[200];

    /* Smart large object file descriptor */
    SQLLEN          lofd;
    SQLLEN          lofd_valsize = 0;

    /* Smart large object pointer structure */
    SQLCHAR*        loptr_buffer;
    SQLSMALLINT     loptr_size;
    SQLLEN          loptr_valsize = 0;

    /* Smart large object specification structure */
    SQLCHAR*        lospec_buffer;
    SQLSMALLINT     lospec_size;
    SQLLEN          lospec_valsize = 0;

    /* Write buffer */
    SQLCHAR*        write_buffer;
    SQLSMALLINT     write_size;
    SQLCHAR*        v_write_buffer;
    SQLSMALLINT     v_write_size;
    SQLLEN          write_valsize = 0;
    SQLLEN          v_write_valsize = 0;
    size_t          status;
    struct stat     statbuf;
    int             fd;

    /* Miscellaneous variables */
    SQLRETURN       rc = 0;
    SQLLEN          in;

    SQLCHAR         verInfoBuffer[BUFFER_LEN];
    SQLSMALLINT     verInfoLen;

    SQLCHAR*        lo_file_name = (SQLCHAR *) "advert.txt";

    SQLCHAR         colname[BUFFER_LEN] = "tlob.c4";
    SQLLEN          colname_size = SQL_NTS;

    SQLINTEGER      mode = LO_RDWR;
    SQLINTEGER      cbMode = 0;

    SQLCHAR*        insertStmt = (SQLCHAR *) "insert into tlob(c4) values(?)";



    /* Allocate the Environment handle */
    rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
    /* Allocate the connection handle */
    rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
    /* Establish the database connection */
    rc = SQLDriverConnect (hdbc, NULL, connStrIn, SQL_NTS, connStrOut, 200, &connStrOutLen, SQL_DRIVER_NOPROMPT);


    /* Allocate the statement handle */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
    

    /* STEP 2.  Get the size of the smart large object specification structure
    **          Allocate a buffer to hold the structure
    **          Create a default smart large object specification structure
    **          Reset the statement parameters
    */


    /* Get the size of a smart large object specification structure */
    rc = SQLGetInfo (hdbc, SQL_INFX_LO_SPEC_LENGTH, &lospec_size,sizeof(lospec_size), NULL);
    /* Allocate a buffer to hold the smart large object specification
       structure*/
    lospec_buffer = malloc (lospec_size);
    if (lospec_buffer == NULL)
    {
        fprintf(stdout, "Failed to allocate memory for the largeobject specification buffer \n");
        goto Exit;
    }

    /* Create a default smart large object specification structure */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0,
                           lospec_buffer, lospec_size, &lospec_valsize);


    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_def_create_spec(?)}", SQL_NTS);

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);


    /* STEP 3.  Initialise the smart large object specification structure with
    **          values for the database column where the smart large object is
    **          being inserted
    **          Reset the statement parameters
    */

    /* Initialise the smart large object specification structure */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
                           BUFFER_LEN, 0, colname, BUFFER_LEN, &colname_size);

    lospec_valsize = lospec_size;

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0, lospec_buffer,
                           lospec_size, &lospec_valsize);

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_col_info(?, ?)}", SQL_NTS);


    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);


    /* STEP 4.  Get the size of the smart large object pointer structure
    **          Allocate a buffer to hold the structure.
    */

    /* Get the size of the smart large object pointer structure */
    rc = SQLGetInfo (hdbc, SQL_INFX_LO_PTR_LENGTH, &loptr_size, sizeof(loptr_size), NULL);

    /* Allocate a buffer to hold the smart large object pointer structure */
    loptr_buffer = malloc (loptr_size);
    if (loptr_buffer == NULL)
    {
        fprintf(stdout, "Failed to allocate memory for the smart large object pointer structure buffer \n");
        goto Exit;
    }



    /* STEP 5.  Create a new smart large object.
    **          Reset the statement parameters
    */

    /* Create a new smart large object */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
                           (UDWORD)lospec_size, 0, lospec_buffer, lospec_size, &lospec_valsize);

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
                           (UDWORD)0, 0, &mode, sizeof(mode), &cbMode);

    loptr_valsize = loptr_size;

    rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
                           (UDWORD)loptr_size, 0, loptr_buffer, loptr_size, &loptr_valsize);

    rc = SQLBindParameter (hstmt, 4, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER,
                           (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_create(?, ?, ?, ?)}", SQL_NTS);

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);


    /* STEP 6.  Open the file containing data for the new smart large object
    **          Allocate a buffer to hold the smart large object data
    **          Read data from the input file into the smart large object data buffer
    **          Write data from the data buffer into the new smart large object.
    **          Reset the statement parameters
    */

    /* Get the size of the file containing data for the new smart large object */
    if (stat( (char *)lo_file_name,&statbuf) == -1)
    {
        fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
        exit(1);
    }
    write_size = statbuf.st_size;

    /* Allocate a buffer to hold the smart large object data */
    write_buffer = malloc (write_size + 1);
    if (write_buffer == NULL)
    {
        fprintf(stdout, "Failed to allocate memory for the smart large object data buffer \n");
        goto Exit;
    }

    /* Read smart large object data from file */
    fd = open ((char *) lo_file_name, O_RDONLY);
    if (fd == -1)
    {
        fprintf (stdout, "Error %d creating file descriptor for %s\n", errno, lo_file_name);
        exit(1);
    }
    status = read (fd, write_buffer, write_size);
    if (status < 0)
    {
        fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
    }
    if (close(fd) < 0)
    {
        fprintf (stdout, "Error %d closing the file %s\n", errno, lo_file_name);
        exit(1);
    }
//方式1 使用文件指针插入
    write_buffer[write_size] = '\0';
    write_valsize = write_size;
//方式2 使用buffer插入
    v_write_buffer ="abcd";
    v_write_valsize=5;
    /* Write data from the data buffer into the new smart large object */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
                           (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);

    //rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,(UDWORD)write_size, 0, write_buffer, write_size, &write_valsize);
    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,(UDWORD)5, 0, v_write_buffer,5, &v_write_valsize);
    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_write(?, ?)}", SQL_NTS);


    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);




    /* STEP 7.  Insert the new smart large object into the database.
    **          Reset the statement parameters
    */

    /* Insert the new smart large object into the database */
    loptr_valsize = loptr_size;

    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
                           (UDWORD)loptr_size, 0, loptr_buffer, loptr_size, &loptr_valsize);

    rc = SQLExecDirect (hstmt, insertStmt, SQL_NTS);

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);


    /* STEP 8.  Close the smart large object. */

    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                           (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_close(?)}", SQL_NTS);



    /* STEP 9. Free the allocated buffers */
    if (lospec_buffer)
        free (lospec_buffer);
    if (loptr_buffer)
        free (loptr_buffer);
    if (write_buffer)
        free (write_buffer);

Exit:

    /* Close the statement handle */
    SQLFreeStmt (hstmt, SQL_CLOSE);

    /* Free the statement handle */
    SQLFreeHandle (SQL_HANDLE_STMT, hstmt);

    /* Disconnect from the data source */
    SQLDisconnect (hdbc);

    /* Free the environment handle and the database connection handle */
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
    SQLFreeHandle (SQL_HANDLE_ENV, henv);
    return (rc);
}

2 查询

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /*NO_WIN32*/

#include "infxcli.h"

#define BUFFER_LEN  25
#define ERRMSG_LEN  200

SQLINTEGER checkError (SQLRETURN       rc,
                       SQLSMALLINT     handleType,
                       SQLHANDLE       handle,
                       SQLCHAR*            errmsg)
{
    SQLRETURN       retcode = SQL_SUCCESS;

    SQLSMALLINT     errNum = 1;
    SQLCHAR         sqlState[6];
    SQLINTEGER      nativeError;
    SQLCHAR         errMsg[ERRMSG_LEN];
    SQLSMALLINT     textLengthPtr;


    if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
    {
        while (retcode != SQL_NO_DATA)
        {
            retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState,
                                     &nativeError, errMsg, ERRMSG_LEN,
                                     &textLengthPtr);

            if (retcode == SQL_INVALID_HANDLE)
            {
                fprintf (stderr, "checkError function was called with an invalid handle!!\n");
                return 1;
            }

            if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
                fprintf (stderr, "ERROR: %d:  %s : %s \n", nativeError,
                         sqlState, errMsg);

            errNum++;
        }

        fprintf (stderr, "%s\n", errmsg);
        return 1;
    }
    else
        return 0;
}




int main (long         argc,
          char*        argv[])
{
	SQLCHAR*   defDsn = "DRIVER=/home/gbasedbt/install_dir/330_2/lib/cli/iclit09b.so;DLOC=zh_CN.57372;CLOC=zh_CN.57372;DB=test;SRVR=er01;UID=root;PWD=123456";


    /* Handles */
    SQLHDBC        hdbc;
    SQLHENV        henv;
    SQLHSTMT       hstmt;
    SQLCHAR         connStrOut[200];
	SQLSMALLINT     connStrOutLen;
    /* Smart large object file descriptor */
    SQLLEN         lofd = 0;
    SQLLEN         lofd_valsize = 0;

    /* Smart large object pointer structure */
    SQLCHAR*       loptr_buffer;
    SQLSMALLINT    loptr_size = 0;
    SQLLEN         loptr_valsize = 0;

    /* Smart large object status structure */
    SQLCHAR*       lostat_buffer;
    SQLSMALLINT    lostat_size = 0;
    SQLLEN         lostat_valsize = 0;

    /* Smart large object data */
    SQLCHAR*       lo_data;
    SQLLEN         lo_data_valsize = 0;

    /* Miscellaneous variables */
    SQLCHAR        dsn[20]; /*name of the DSN used for connecting to the database*/
    SQLRETURN      rc = 0;
    SQLINTEGER     in;

    SQLCHAR        verInfoBuffer[BUFFER_LEN];
    SQLSMALLINT    verInfoLen;

    SQLCHAR*       selectStmt = (SQLCHAR *) "SELECT c1 FROM t1 limit 1";
    SQLINTEGER     mode = LO_RDONLY;
    SQLLEN         lo_size = 0;
    SQLLEN         cbMode = 0, cbLoSize = 0;

    /*  STEP 1. Get data source name from command line (or use default)
    **          Allocate the environment handle and set ODBC version
    **          Allocate the connection handle
    **          Establish the database connection
    **          Get version information from the database server
    **          -- if version < 9.x (not UDO enabled), exit with error message
    **          Allocate the statement handle
    */


   

    /* Allocate the Environment handle */
    rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (rc != SQL_SUCCESS)
    {
        fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
        return (1);
    }

    /* Set the ODBC version to 3.0 */
    rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
        return (1);


    /* Allocate the connection handle */
    rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
        return (1);

    /* Establish the database connection */
    rc = SQLDriverConnect (hdbc, NULL, (char*)defDsn , SQL_NTS, connStrOut, 200, &connStrOutLen, SQL_DRIVER_NOPROMPT);

    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!\n"))
        return (1);

    /* Get version information from the database server
       If version < 9.x (not UDO enabled), exit with error message */

    rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN,
                     &verInfoLen);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
        return 1;

    if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0)
    {
        fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
        return 1;
    }

    /* Allocate the statement handle */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );

    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
        return (1);

    fprintf (stdout, "STEP 1 done...connected to database\n");


    /* STEP 2.  Select a smart-large object from the database
     **          -- the select  statement executed is -
     **          "SELECT advert FROM item WHERE item_num = 1004"
     */

    /* Execute the select statement */
    rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 2 done...select statement executed...smart large object retrieved from the databse\n");


    /* STEP 3.  Get the size of the smart large object pointer structure
     **          Allocate a buffer to hold the structure.
     **          Get the smart large object pointer structure from the database
     **          Close the result set cursor
     */

    /* Get the size of the smart large object pointer structure */
    rc = SQLGetInfo (hdbc, SQL_INFX_LO_PTR_LENGTH, &loptr_size,
                     sizeof(loptr_size), NULL);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc,
                    (SQLCHAR *) "Error in Step 3 -- SQLGetInfo failed\n"))
        goto Exit;

    /* Allocate a buffer to hold the smart large object pointer structure */
    loptr_buffer = malloc (loptr_size);

    /* Bind the smart large object pointer structure buffer allocated to
     * the column in the result set & fetch it from the database */
    rc = SQLBindCol (hstmt, 1, SQL_C_BINARY, loptr_buffer, loptr_size,
                     &loptr_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt,
                    (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
        goto Exit;

    rc = SQLFetch (hstmt);
    if (rc == SQL_NO_DATA_FOUND)
    {
        fprintf (stdout, "No Data Found\nExiting!!\n");
        goto Exit;
    }
    if (checkError (rc, SQL_HANDLE_STMT, hstmt,
                    (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
        goto Exit;

    /* Close the result set cursor */
    rc = SQLCloseCursor (hstmt);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt,
                    (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 3 done...smart large object pointer structure fetched from the database\n");


    /* STEP 4.  Use the smart large object's pointer structure to open it
    **          and obtain the smart large object file descriptor.
    **          Reset the statement parameters
    */

    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_LONG,
                           SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd),
                           &lofd_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
        goto Exit;

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_FIXED, (UDWORD)loptr_size, 0, loptr_buffer,
                           loptr_size, &loptr_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 2)\n"))
        goto Exit;

    rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_LONG,
                           SQL_INTEGER, (UDWORD)0, 0, &mode, sizeof(mode), &cbMode);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 3)\n"))
        goto Exit;

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{? = call  ifx_lo_open(?, ?)}", SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
        goto Exit;

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLFreeStmt failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 4 done...smart large object opened... file descriptor obtained\n");


    /* STEP 5.  Get the size of the smart large object status structure
    **          Allocate a buffer to hold the structure.
    **          Get the smart large object status structure from the database
    **          Reset the statement parameters
    */

    /* Get the size of the smart large object status structure */
    rc = SQLGetInfo (hdbc, SQL_INFX_LO_STAT_LENGTH, &lostat_size,
                     sizeof(lostat_size), NULL);
    if (checkError (rc, SQL_HANDLE_DBC, hdbc,
                    (SQLCHAR *) "Error in Step 5 -- SQLGetInfo failed\n"))
        goto Exit;

    /* Allocate a buffer to hold the smart large object status structure. */
    lostat_buffer = malloc(lostat_size);


    /* Get the smart large object status structure from the database. */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG,
                           SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd),
                           &lofd_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
        goto Exit;

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_FIXED, (UDWORD)lostat_size, 0,
                           lostat_buffer, lostat_size, &lostat_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 2)\n"))
        goto Exit;

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_stat(?, ?)}", SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
        goto Exit;

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLFreeStmt failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 5 done...smart large object status structure fetched from the database\n");


    /* STEP 6.  Use the smart large object's status structure to get the size
    **          of the smart large object
    **          Reset the statement parameters
    */

    /* Use the smart large object status structure to get the size
       of the smart large object. */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_FIXED, (UDWORD)lostat_size, 0,
                           lostat_buffer, lostat_size, &lostat_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
        goto Exit;

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_BIGINT,
                           (UDWORD)0, 0, &lo_size, sizeof(lo_size), &cbLoSize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
        goto Exit;

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_stat_size(?, ?)}",
                        SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
        goto Exit;

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLFreeStmt failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 6 done...smart large object size = %ld bytes\n", lo_size);



    /* STEP 7.  Allocate a buffer to hold the smart large object's data
     **          Read the smart large object's data using its file descriptor
     **          Null-terminate the last byte of the smart large-object's data
     **          Print out the contents of the smart large object
     **          Reset the statement parameters
     */

    /* Allocate a buffer to hold the smart large object's data chunks */
    lo_data = malloc (lo_size + 1);

    /* Read the smart large object's data */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                           (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed (param 1)\n"))
        goto Exit;

    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_CHAR,
                           lo_size, 0, lo_data, lo_size, &lo_data_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed (param 2)\n"))
        goto Exit;

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_read(?, ?)}", SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLExecDirect failed\n"))
        goto Exit;

    /* Null-terminate the last byte of the smart large objects data */
    lo_data[lo_size] = '\0';

    /* Print the contents of the smart large object */
    fprintf (stdout, "Smart large object contents are.....\n\n\n%s\n\n\n",
             lo_data);

    /* Reset the statement parameters */
    rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLFreeStmt failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 7 done...smart large object read completely\n");

    /* STEP 8.  Close the smart large object.  */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                           (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLBindParameter failed\n"))
        goto Exit;

    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_close(?)}", SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLExecDirect failed\n"))
        goto Exit;

    fprintf (stdout, "STEP 8 done...smart large object closed\n");


    /* STEP 9. Free the allocated buffers */
    free (loptr_buffer);
    free (lostat_buffer);
    free (lo_data);

    fprintf (stdout, "STEP 9 done...smart large object buffers freed\n");

Exit:
    /* CLEANUP: Close the statement handle
    **          Free the statement handle
    **          Disconnect from the datasource
    **          Free the connection and environment handles
    **          Exit
    */

    /* Close the statement handle */
    SQLFreeStmt (hstmt, SQL_CLOSE);

    /* Free the statement handle */
    SQLFreeHandle (SQL_HANDLE_STMT, hstmt);

    /* Disconnect from the data source */
    SQLDisconnect (hdbc);

    /* Free the environment handle and the database connection handle */
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
    SQLFreeHandle (SQL_HANDLE_ENV, henv);

    fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
    in = getchar ();
    return (rc);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值