TPT api UpdateDriver why fail ??

Tools
N/A

TPT api UpdateDriver why fail ??

/* teradata odbc sample */

#include <stdio.h>
#include <iostream>
#include "connection.h"
#include "schema.h"
#include "DMLGroup.h"
#include "sqlunx.h"
#include <sql.h>
#include <sqlext.h>
#include <stdlib.h>

#define TRUE  1
#define FALSE  0

#define MAX_STR  256
#define MAXLINE  1000000
#define MAX_NAME_LEN 50
#define MAXCOLS  2048

#define MAXCONNECTIONS 100

using namespace std;
using namespace teradata::client::API;

struct COL_INFO{
    char colname[32];
    int coltype;
    int colnamelen;
    int nullable;
    int scale;
    int collen;
};  

struct ENV_HANDLE{
    HENV henv;
    HDBC hdbc;
    HSTMT hstmt;

    char  dbName[MAX_STR];
    char  user[MAX_STR];
    char  passwd[MAX_STR];
    char  tableName[MAX_STR];
    COL_INFO* colInfo;
};

int main(void)
{
    int            i;
    ENV_HANDLE     env;
    int            len, returnValue;
    int            rc;
    SWORD          nresultcols;
    char           db[MAX_STR];
    SQLSMALLINT    dblen;
    TD_ErrorType   errorType;
   
    Connection *conn = new Connection();
    char* errorMsg = NULL;
    SQLCHAR           dbname[256] = "testdsn";
    SQLCHAR           ip[256] = "192.168.10.112";
    SQLCHAR           user[256] = "truser2";
    SQLCHAR           pass[256] = "trpass2";
    SQLCHAR           query[256] = "select * from coldata_test where 1 = 0";
   
    memset(&env, 0x00, sizeof(ENV_HANDLE) );
    rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env.henv);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("fail Alloc Handle\n");

    rc = SQLSetEnvAttr(env.henv, SQL_ATTR_ODBC_VERSION,(void *)SQL_OV_ODBC3,4);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("SEtEnvAttr fail \n");

    rc = SQLAllocHandle(SQL_HANDLE_DBC,env.henv, &env.hdbc);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("SQLAllocHandle fail ");   

    rc = SQLConnect(env.hdbc,
    dbname, 14,
    user,7 ,
    pass, 7);

    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("connect fail\n");

    printf("connection ok\n");

    rc = SQLGetInfo(env.hdbc, SQL_ODBC_VER, &db, (SWORD)sizeof(db), &dblen);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("GetInfo ODBC VERSION");

    cout <<"ODBC version        = -" << (char *)db << "-" << endl;   

    rc = SQLGetInfo(env.hdbc, SQL_DBMS_NAME, &db, (SWORD) sizeof(db), &dblen);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
     printf("DBMS NAME GET FAIL");

    cout <<"DBMS name           = -" << (char *)db << "-" << endl;

    rc = SQLGetInfo(env.hdbc, SQL_DBMS_VER, &db, (SWORD) sizeof(db), &dblen);
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        printf("faile");

    cout <<"DBMS version        = -" << (char *)db << "-" << endl;

    rc = SQLAllocHandle(SQL_HANDLE_STMT, env.hdbc, &env.hstmt);   
    if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
       printf("handler alloc fail \n");
   
    rc = SQLExecDirect(env.hstmt, query , SQL_NTS);
    if (rc != SQL_SUCCESS)
    {
       printf("fail execute query \n");
    }
   
    do{

        rc = SQLNumResultCols(env.hstmt, &nresultcols);
        env.colInfo = (COL_INFO*)malloc( sizeof(COL_INFO) * nresultcols );
        memset( env.colInfo, 0x00, sizeof(COL_INFO) * nresultcols );
        for( i=0; i< nresultcols; i++)
        {
            SQLDescribeCol( env.hstmt, i+1, (SQLCHAR*)env.colInfo[i].colname, (SWORD) sizeof(env.colInfo[i].colname),
                            (SWORD*)&env.colInfo[i].colnamelen, (SWORD*)&env.colInfo[i].coltype, (SQLUINTEGER*)&env.colInfo[i].collen,
                            (SWORD*)&env.colInfo[i].scale, (SWORD*)&env.colInfo[i].nullable);        
            printf("name [%s], len [%d], type [%d]\n", env.colInfo[i].colname, env.colInfo[i].collen, env.colInfo[i].coltype);
        }

    }
    while((rc = SQLMoreResults(env.hstmt)) == 0 );

    conn->AddAttribute(TD_SYSTEM_OPERATOR, TD_UPDATE);
    conn->AddAttribute(TD_TRACE_OUTPUT,"update.txt");
    conn->AddArrayAttribute(TD_TRACE_LEVEL, 2, TD_OPER_ALL,TD_GENERAL, NULL);
    //conn->AddAttribute(TD_BUFFER_MODE,"yes"); 

    conn->AddAttribute(TD_TDP_ID, (char*)ip );
    conn->AddAttribute(TD_USER_NAME, (char*)user );
    conn->AddAttribute(TD_USER_PASSWORD, (char*)pass );
    conn->AddArrayAttribute(TD_TARGET_TABLE, 1,"coldata_test", NULL);
    conn->AddArrayAttribute(TD_WORK_TABLE , 1,"coldata_test_wt", NULL);
    conn->AddAttribute(TD_LOG_TABLE,"log_table");
    conn->AddArrayAttribute(TD_ERROR_TABLE_1,1,"coldata_test_e1",NULL);
    conn->AddArrayAttribute(TD_ERROR_TABLE_2,1,"coldata_test_e2",NULL);
    Schema * schema = new Schema("input");
    for( i= 0; i< nresultcols; i++){
    //schema->AddColumn(env.colInfo[i].colname, (TD_DataType)env.colInfo[i].coltype, env.colInfo[i].collen);
    schema->AddColumn(env.colInfo[i].colname, TD_VARCHAR, env.colInfo[i].collen);
    }
    conn->AddSchema(schema);
    TD_Index dmlGroupIndex = 0;
    DMLGroup * dmlGr = new DMLGroup();
    memset(query , 0x00, sizeof( query ) );
    sprintf((char*)query, "INSERT INTO coldata_test ( :%s, :%s, :%s ); ", (char*)env.colInfo[0].colname, (char*)env.colInfo[1].colname, (char*)env.colInfo[2].colname );
    printf("%s\n", query);
    dmlGr->AddStatement((char*)query);
    dmlGr->AddDMLOption(MARK_DUPLICATE_ROWS);
    returnValue = conn->AddDMLGroup(dmlGr,&dmlGroupIndex);
    cout << "DMLGroups added with status " << returnValue << endl;

    returnValue = conn->Initiate();
    cout << "Driver Initiated with status " << returnValue << endl;
    if( returnValue < TD_Error ){
        printf("Initiate OK!\n");
        char rowBuffer[1024];
        TD_Length bytes;
        int loadStatus = 0;
        memset(rowBuffer, 0x00, 765);
        strcpy(rowBuffer,"col01,col02,col03\n");
        returnValue = conn->UseDMLGroups(&dmlGroupIndex,1 );
        if( returnValue >= TD_Error){
            printf("UseDMLGroup fail \n");
        }
        cout << "Sending First Row" << endl;
        returnValue = conn->PutRow( rowBuffer ,18);
        if( returnValue < TD_Error ){
            returnValue = conn->EndAcquisition();
            if( returnValue < TD_Error ){
                returnValue = conn->ApplyRows();
            }
            else{
                conn->GetErrorInfo(&errorMsg,&errorType);
                if ( errorMsg != NULL ){
                    cout << errorMsg << endl;
                    cout << "Type: " << errorType << endl;
                }else{
                    cout << "No Error Info Available" << endl;
                }
            }
        }
        else{
            conn->GetErrorInfo(&errorMsg,&errorType);
            if ( errorMsg != NULL ){
                cout << errorMsg << endl;
                cout << "Type: " << errorType << endl;
            }
            else{
                cout << "No Error Info Available" << endl;
            }
        }

    }
    else{
        cout << "Error occured during Acquisition" << endl;
        conn->GetErrorInfo(&errorMsg,&errorType);
        if ( errorMsg != NULL ){
            cout << errorMsg << endl;
            cout << "Type: " << errorType << endl;
        }else{
            cout << "No Error Info Available" << endl;
        }
    }
    returnValue = conn->Terminate();
   if ( returnValue >= TD_Error )
   {
      //Get Error Information
      cout << "Error occured during Terminate" << endl;
      conn->GetErrorInfo(&errorMsg,&errorType);
      if ( errorMsg != NULL ){
         cout << errorMsg << endl;
         cout << "Type: " << errorType << endl;
      }else{
         cout << "No Error Info Available" << endl;
      }
   }

   delete dmlGr;
   delete schema;
   delete conn;
    return 0;
}

teradata version : 13.10

Use API  = odbc , TPT API

Table Schema :

           create table coldata_test(

               col01 varchar(255),

               col02 varchar(255),

               col03 varchar(255)

            )

execute : Not Fail Message

               create error table  ANd errorcode = 2673

why not execute success??

Tags (2)
2 REPLIES
Teradata Employee

Re: TPT api UpdateDriver why fail ??

TD DBS is at version 13.10.

What version of TPTAPI are you using?

"create error table" is not something that TPTAPI would submit, so that is probably coming from your application.

Another thing to consider: I do not believe running ODBC and CLIv2 in the same application works, or is supported. TPTAPI operators use CLIv2. That may not be the cause of the issue you are facing, but it is still not clear exactly what is the error here.

A 2673 means the data you are sending does not meet the USING clause from the DML statement.

You would need to provide more information in order to determine the exact problem, but again I am not sure you can mix ODBC and CLIv2.

-- SteveF
N/A

Re: TPT api UpdateDriver why fail ??

g++&nbsp; -c -o ./sample.o sample.cpp -I/opt/teradata/client/13.10/odbc_64/include -I/opt/teradata/client/13.10/tbuild/tptapi/inc

g++ ./sample.o -L/opt/teradata/client/13.10/odbc_64/lib -L/opt/teradata/client/13.10/tbuild/lib64 -L/usr/lib64/libstdc++.so.5 -L/usr/lib64 -L/lib64 -ltelapi -lpxicu -lodbc -lodbcinst -lddicu24

kahan@s10-1310:~/teraodbc> ./a.out
connection ok
ODBC version        = -03.52.0000-
DBMS name           = -Teradata-
DBMS version        = -13.10.0014  13.10.00.14-
name [col01], len [5], type [1]
name [col02], len [5], type [1]
name [col03], len [5], type [1]
INSERT INTO coldata_test ( :col01, :col02, :col03 );
DMLGroups added with status 0
Driver Initiated with status 3
Initiate OK!
Sending First Row
kahan@s10-1310:~/teraodbc>

i need connect info , table info, column info 

So. Use ODBC