TDVサーバーに接続するためのC++ UNIXコードサンプル
次の例は、UNIX用のC++で記述された小さなテストプログラムを示しています。文字列「dsn」は、DSN形式ではなく接続文字列形式を使用します。これは、データベースに接続するためのSQLDriverConnect呼び出しで使用されます。
{code}
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <string.h>
int SQLSuccess(SQLRETURN rc) {
return (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO);
}
void extract_error(const char* sqlFunc, SQLHANDLE handle, SQLSMALLINT type) {
SQLINTEGER i = 0;
SQLINTEGER native;
SQLCHAR state[7];
SQLCHAR text[256];
SQLSMALLINT len;
SQLRETURN ret;
memset(state, 0, sizeof(state));
memset(text, 0, sizeof(text));
do {
ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len);
if (SQL_SUCCEEDED(ret)) {
printf("%s:%ld:%ld:%s\n", state, i, native, text);
}
} while (ret == SQL_SUCCESS);
exit(0);
}
int fetchResultSet(SQLHSTMT stmt)
{
SQLLEN indicator;
SQLRETURN ret;
char buf[512];
SQLSMALLINT columns;
int i=0,rows=0;
SQLNumResultCols(stmt, &columns);
//Fetch all data
while(1){
ret = SQLFetch(stmt);
if ( SQL_NO_DATA == ret ){
break;
}else if (!SQLSuccess(ret)) {
extract_error("SQLFetch", stmt, SQL_HANDLE_STMT);
}
rows++;
for(i=1;i<columns;i++){
ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
if (!SQLSuccess(ret)) {
extract_error("SQLGetData", stmt, SQL_HANDLE_STMT);
}
}
}
return rows;
}
int main(int argc, char * argv[])
{
printf("sizeof(SQLULEN)=%d\n",sizeof(SQLULEN));
printf("sizeof(SQLUINTEGER)=%d\n",sizeof(SQLUINTEGER));
printf("sizeof(SQLUSMALLINT)=%d\n",sizeof(SQLUSMALLINT));
SQLRETURN ret;
SQLCHAR tableCat[64];
SQLCHAR tableSchem[64];
SQLCHAR tableName[64];
SQLCHAR tableType[64];
SQLCHAR remarks[64];
SQLLEN Str_Len;
SQLSMALLINT colCount=0;
SQLCHAR dsn[] = "Driver={TDV 8.0};Server=localhost;Port=9401;Domain=composite;dataSource=system;user=admin;password=admin;validateRemoteHostname=false;connectTimeout=3000;enableFastExec=false";
SQLCHAR query[] ="select * from ALL_TABLES";
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*) dsn, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
if (!SQLSuccess(ret)) {
extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
}
SQLCHAR ver[512];
SQLGetInfo(dbc,
SQL_DRIVER_NAME,
ver,
512,
NULL);
printf("%s\n",ver);
SQLGetInfo(dbc,
SQL_DRIVER_VER,
ver,
512,
NULL);
printf("%s\n",ver);
SQLGetInfo(dbc,
SQL_DBMS_NAME,
ver,
512,
NULL);
printf("%s\n",ver);
SQLGetInfo(dbc,
SQL_DBMS_VER,
ver,
512,
NULL);
printf("%s\n",ver);
ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
if (!SQLSuccess(ret)) {
extract_error("SQLAllocHandle", dbc, SQL_HANDLE_DBC);
}
ret = SQLTables(stmt,(SQLCHAR*)"",0,(SQLCHAR*)"",0,(SQLCHAR*)"",0,(SQLCHAR*)"TABLE",SQL_NTS);
ret= SQLNumResultCols(stmt,&colCount);
ret = SQLBindCol(stmt, 1, SQL_C_CHAR, tableCat, sizeof(tableName), &Str_Len);
ret = SQLBindCol(stmt, 2, SQL_C_CHAR, tableSchem, sizeof(tableName), &Str_Len);
ret = SQLBindCol(stmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), &Str_Len);
ret = SQLBindCol(stmt, 4, SQL_C_CHAR, tableType, sizeof(tableName), &Str_Len);
ret = SQLBindCol(stmt, 5, SQL_C_CHAR, remarks, sizeof(tableName), &Str_Len);
while( (ret=SQLFetch(stmt))==SQL_SUCCESS){
printf("%s\n",tableName);
}
ret = SQLExecDirect(stmt, (SQLCHAR*) query, SQL_NTS);
//ret= SQLPrepareW(stmt,(SQLWCHAR*)query,10);
ret= SQLNumResultCols(stmt,&colCount);
if (!SQLSuccess(ret)) {
extract_error("SQLExecDirect", stmt, SQL_HANDLE_STMT);
}
int totalRows = fetchResultSet(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
SQLDisconnect(dbc);
ret = SQLFreeHandle(SQL_HANDLE_DBC, dbc);
ret = SQLFreeHandle(SQL_HANDLE_ENV, env);
printf("Execute query completed, total rows %d.\n",totalRows);
return 0;
}
{code}