SQLGetTypeInfo

It returns information about data types supported by the data source. The driver returns the information in the form of an SQL result set.

Syntax

SQLRETURN SQL_API SQLGetTypeInfo( SQLHSTMT aStmt, SQLSMALLINT aSqlType)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStmt Input Statement handle
SQLSMALLINT aSqlType Input Data type

Return Vales

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_ERROR Abnormal termination. Not supported type
SQL_INVALID_HANDLE Abnormal termination. Invalid statement handle

Sample Code

In the main code of the link, change SQLStatistics with the code below.

if( SQLGetTypeInfo( sStmt, SQL_ALL_TYPES ) == SQL_ERROR )
{
printError(sEnv, sCon, sStmt, "SQL Free Stmt Handle error");
}
printResultTable( sStmt );

SQLNativeSQL

It returns the SQL string as modified by the driver. However, it does not execute the SQL statement.

Syntax

SQLRETURN SQL_API SQLNativeSql( SQLHDBC aCon,
SQLCHAR *aSqlStrIn,
SQLINTEGER aSqlStrInLength,
SQLCHAR *aSqlStrOut,
SQLINTEGER aSqlStrOutMax,
SQLINTEGER *aSqlStrOutLength )

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHDBC aCon Input Not used
SQLCHAR* aSqlStrIn Input SQL statement
SQLINTEGER aSqlStrInLength Input Length of SQL statement
SQLCHAR* aSqlStrOut Output Converted SQL statement
SQLINTEGER aSqlStrOutMax Input Buffer size of converted SQL statement/ SQL_NTS or numbers should be entered.
SQLINTEGER* aSqlStrOutLength Output Length of converted SQL statement/ Back data will be lost if it is larger than the buffer length.

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_ERROR Abnormal termination/ The length of character string was less than 0, but not equal to SQL_NTS.

Sample Code

SQLNativeSql(gDbc, (SQLCHAR*)sIn, (SQLINTEGER)strlen(sIn), (SQLCHAR*)sOut, BufLen, &sOutLen);

SQLNumParams

It returns the number of parameters in an SQL statement.

Syntax

SQLRETURN SQL_API SQLNumParams (SQLHSTMT aStmt, SQLSMALLINT *aParamCount)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStmt input Statement handle
SQLSMALLINT* aParamCount Output Number of parameters

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_INVALID_HANDLE Abnormal termination/ Unallocated statement handle

Sample Code

Refer to SQLPrepare for more information.

SQLNumResultCols

It returns the number of columns in a result set.

Syntax

SQLRETURN SQL_API SQLNumResultCols (SQLHSTMT aStmt, SQLSMALLINT *aColumnCount)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStmt Input Statement handle
SQLSMALLINT* aColumnCount Output Number of columns in the result set

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_INVALID_HANDLE Abnormal termination/ Unallocated statement handle

Sample Code Refer to SQLTables for more information.

SQLPrepare

It prepares an SQL string for execution.

Syntax

SQLRETURN SQL_API SQLPrepare(SQLHSTMT aStatementHandle, SQLCHAR *aStatementText, SQLINTEGER aTextLength)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStatementHandle Input Statement handle
SQLCHAR* aStatementText Output Statement text
SQLINTEGER aTextLength Input Length of statement text in characters

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_INVALID_HANDLE Abnormal termination/ Invalid statement handle

Diagnostic Codes

Table. Diagnostic Codes

Code Error Description
08S01 Communication link failure Communication link failure between databases
HY090 Invalid string or buffer length The argument was less than or equal to 0.

Sample Code

Refer to Data Input and Output for more information.

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

struct bindData//data binding struct
{
SQLSMALLINT mTargetType;
char mTargetValuePtr[30];
SQLINTEGER mBufferMax;
SQLLEN mBufferLength;
};

void printError(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt, char* msg);//error print function
void connectDB(SQLHENV* aEnv, SQLHDBC* aCon, SQLHSTMT* aStmt);//db connect function
void disconnectDB(SQLHENV* aEnv, SQLHDBC* aCon, SQLHSTMT* aStmt);//db disconnect function
void createTable(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt);//sqltest table create

int main()
{
SQLHENV sEnv = SQL_NULL_HANDLE;
SQLHDBC sCon = SQL_NULL_HANDLE;
SQLHSTMT sStmt = SQL_NULL_HANDLE;
int i = 0;
int sParamCount = 0;
SQL_APPEND_PARAM sParams[10];
char sVarchar[30];
int sVarcharLength = 0;

connectDB(&sEnv, &sCon, &sStmt);
createTable(sEnv, sCon, sStmt);

if( SQLPrepare( sStmt, (SQLCHAR*)"insert into sqltest values(?, ?)", SQL_NTS) == SQL_ERROR )
{
printError( sEnv, sCon, sStmt, "SQL Prepare error");
}

if( SQLNumParams( sStmt, &sParamCount ) == SQL_ERROR )
{
printError( sEnv, sCon, sStmt, "SQL Num Params");
}

printf("number of params : %d\n", sParamCount);

if( SQLBindParameter( sStmt,
1,
SQL_PARAM_INPUT,
SQL_C_SBIGINT,
SQL_BIGINT,
0,
0,
&i,
0,
NULL ) == SQL_ERROR )
{
printError( sEnv, sCon, sStmt, "SQL Bind Parameter");
}

if( SQLBindParameter( sStmt,
2,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
0,
0,
sVarchar,
0,
(SQLLEN*)&sVarcharLength ) == SQL_ERROR )
{
printError( sEnv, sCon, sStmt, "SQL Bind Parameter");
}

for( i = 0; i < 10; i++)
{
sprintf(sVarchar, "varchar-%d", i);
sVarcharLength = strlen(sVarchar);

if( SQLExecute( sStmt ) == SQL_ERROR )
{
printError( sEnv, sCon, sStmt, "SQL Execute");
}
printf("insert into sqltest values(%d, %s) success\n", i, sVarchar);
}

disconnectDB(sEnv, sCon, sStmt);
return 0;
}

void createTable(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt)
{
char* sSql = "";

sSql = "drop table sqltest";
if( SQLExecDirect( aStmt, (SQLCHAR*)sSql, strlen(sSql)) == SQL_ERROR )
{
printf("SQL Exec Direct error(%s)\n",sSql);
}

sSql = "create table sqltest(int1 integer, char1 varchar(30))";
if( SQLExecDirect( aStmt, (SQLCHAR*)sSql, strlen(sSql)) == SQL_ERROR )
{
printError(aEnv, aCon, aStmt, "SQL Exec Direct error");
}
printf("%s success\n", sSql);

}

void connectDB(SQLHENV* aEnv, SQLHDBC* aCon, SQLHSTMT* aStmt)
{
char sConnStr[40] = "DSN=127.0.0.1;CONNTYPE=1;PORT_NO=9875";

if( SQLAllocEnv(aEnv) == SQL_ERROR )
{
printError(*aEnv, NULL, NULL, "SQL Alloc Env Handle error\n");
}

if( SQLAllocConnect(*aEnv, aCon) == SQL_ERROR )
{
printError(*aEnv, *aCon, NULL, "SQL Alloc Con Handle error\n");
}

if( SQLDriverConnect(*aCon, NULL,
(SQLCHAR*)sConnStr, SQL_NTS,
NULL, 0,
NULL, SQL_DRIVER_NOPROMPT) == SQL_ERROR )
{
printError(*aEnv, *aCon, NULL, "SQL Driver Connect error");
}

if( SQLAllocStmt(*aCon, aStmt) == SQL_ERROR )
{
printError(*aEnv, *aCon, *aStmt, "SQL Alloc Stmt Handle error");
}
}

void disconnectDB(SQLHENV* aEnv, SQLHDBC* aCon, SQLHSTMT* aStmt)
{
if( SQLFreeStmt( aStmt, SQL_DROP ) == SQL_ERROR )
{
printError(aEnv, aCon, aStmt, "SQL Free Stmt Handle error");
}

if( SQLDisconnect( aCon ) == SQL_ERROR )
{
printError(aEnv, aCon, NULL, "SQL Disconnect error");
}

if( SQLFreeConnect( aCon ) == SQL_ERROR )
{
printError(aEnv, aCon, NULL, "SQL Free Con Handle error");
}

if( SQLFreeEnv( aEnv ) == SQL_ERROR )
{
printError(aEnv, NULL, NULL, "SQL Free Env Handle error");
}
}

void printError(SQLHENV aEnv, SQLHDBC aCon, SQLHSTMT aStmt, char* msg)
{
SQLINTEGER sErrNo;
SQLSMALLINT sMsgLength;
SQLCHAR sErrMsg[1024];
SQLCHAR sSqlCode[6];

printf("%s\n", msg);
if( SQLError(aEnv, aCon, aStmt, sSqlCode, &sErrNo, sErrMsg, 1024, &sMsgLength) == SQL_SUCCESS )
{
printf("SQL[%s] Machbase[%d] - %s\n", sSqlCode, sErrNo, sErrMsg);
}
exit(-1);
}

SQLPrimaryKeys

It returns the column names that make up the primary key for a table. Log table always provides _arrival_time as the primary key due to the characteristics of time series data.

Syntax

SQLRETURN SQL_API SQLPrimaryKeys (SQLHSTMT aStmt,
SQLCHAR *aTableQualifier,
SQLSMALLINT aTableQualifierLength,
SQLCHAR *aTableOwner,
SQLSMALLINT aTableOwnerLength,
SQLCHAR *aTableName,
SQLSMALLINT aTableNameLength)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStmt Input Statement handle
SQLCHAR* aTableQualifier Input Not used
SQLSMALLINT aTableQualifierLength Input Not used
SQLCHAR* aTableOwner Input User name
SQLSMALLINT aTableOwnerLength Input Length of user name
SQLCHAR* aTableName Input Table name
SQLSMALLINT aTableNameLength Input Length of table name

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_ERROR Abnormal termination
SQL_INVALID_HANDLE Abnormal termination/Invalid statement handle

Diagnostic Codes

Table. Diagnostic Codes

Code Error Description
HY009 Invalid use of null pointer Unallocated table name
HY090 Invalid string or buffer length Length of table name was less than or equal to 0.

Sample Code

SQLBindCol(hstmt, 3, SQL_C_CHAR, strPkTable,
TAB_LEN, &lenPkTable);
SQLBindCol(hstmt, 4, SQL_C_CHAR, strPkCol,
COL_LEN, &lenPkCol);
SQLBindCol(hstmt, 5, SQL_C_SSHORT, &iKeySeq,
TAB_LEN, &lenKeySeq);

SQLPrimaryKeys(gStmt,
NULL, 0, // Catalog name
NULL, 0, // Schema name
(SQLCHAR*)sStrTable, SQL_NTS); // Table name

SQLFetch(gStmt);

printf("Table: %s Column: %s Key Seq: %hd \n", strPkTable, strPkCol, iKeySeq);


Change the SQLStatistics in the main code from the link with the code below.
SQLStatistics


if( SQLPrimaryKeys( sStmt, NULL, 0, NULL, 0, (SQLCAHR*)sTableName, SQL_NTS ) == SQL_ERROR )
{
printError(sEnv, sCon, sStmt, "SQLPrimaryKeys error");
}
printResultTable( sStmt );

SQLRowCount

It returns the number of rows affected by statements. It only works for INSERT and DELETE statements.

Syntax

SQLRETURN SQL_API SQLRowCount (SQLHSTMT aStmt,
SQLLEN *aRowCount)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHSTMT aStmt Input Statement handle
SQLSMALLINT* aRowCount Output Number of rows

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_INVALID_HANDLE Abnormal termination/ Statement handle is NULL.

Sample Code

Refer to SQLExecDirect for more information.

SQLSetConnectAttr

It sets attributes that govern aspects of connections.

Syntax

​SQLRETURN SQL_API SQLSetConnectAttr (SQLHDBC aDbc,
SQLINTEGER aAttribute,
SQLPOINTER aParam,
SQLINTEGER aParamLength)

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHDBC aDbc Input Connection handle
SQLINTEGER aAttribute Input Attribute handle
SQLPOINTER aParam Input Set values
SQLINTEGER aParamLength Input Length of set value

Table of Attribute Value

Table. Table of Attribute Value

Attribute Value Purpose Data type
SQL_ATTR_CONNECTION_TIMEOUT Waiting time while connecting Integer
SQL_ATTR_PORT_NO Port number to connect Integer

Return Values

Table. Return Values

Return Value Description
SQL_SUCCESS Normal termination
SQL_SUCCESS_WITH_INFO Normal termination/Invalid option value
SQL_ERROR Abnormal termination/ Invalid option value
SQL_INVALID_HANDLE Abnormal termination/Invalid statement handle

Diagnostic Codes

Table. Diagnostic Codes

Code Error Description
01S02 Invalid connection string attribute Invalid property value
HYC00 Optional feature not implemented Feature not implemented.

Sample Code

SQLSetConnectAttr(con, SQL_ATTR_AUTOCOMMIT, (void*)SQL_AUTOCOMMIT_ON, 0);

SQLSetDescField

It sets the value of a single field of a descriptor record.

Syntax

SQLRETURN SQL_API SQLSetDescField( SQLHDESC aDescriptorHandle,
SQLSMALLINT aRecordNumber,
SQLSMALLINT aFieldIdentifier,
SQLPOINTER aValue,
SQLINTEGER aBufferLength )

Arguments

Table. Arguments

Type Name of Argument Input/Output Description
SQLHDESC aDescriptorHandle Input Descriptor handle
SQLSMALLINT aRecordNumber Input Number of record
SQLSMALLINT aFieldIdentifier Input Attribute handle
SQLPOINTER aValue Input Input value
SQLINTEGER aBufferLength Input Length of input value

Return Values

Table. Return Values

Return Values Description
SQL_SUCCESS Normal termination
SQL_INVALID_HANDLE Abnormal termination/ Invalid descriptor handle

Diagnostic Codes

Table. Diagnostic Codes

Code Error Description
07009 Invalid descriptor index Line number out of range
HY021 Inconsistent descriptor information Invalid input value
HY024 Invalid attribute value Invalid attribute value
HY091 Invalid descriptor field identifier Invalid input value

Sample Code

SQLSetDescField(hIpd1, 1, SQL_DESC_PARAMETER_TYPE, (SQLPOINTER)SQL_PARAM_INPUT, SQL_IS_INTEGER);

results matching ""

    No results matching ""