#ifdef _WIN32 #include #endif #include #include #include #include int DumpODBCLog(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt) { SQLCHAR szError[501]; SQLCHAR szSqlState[10]; SQLINTEGER nNativeError; SQLSMALLINT nErrorMsg; if ( hStmt ) { while ( SQLError( hEnv, hDbc, hStmt, szSqlState, &nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS ) { printf( "%s\n", szError ); } } if ( hDbc ) { while ( SQLError( hEnv, hDbc, 0, szSqlState, &nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS ) { printf( "%s\n", szError ); } } if ( hEnv ) { while ( SQLError( hEnv, 0, 0, szSqlState, &nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS ) { printf( "%s\n", szError ); } } return 1; } int main(int argc, char *argv[]) { SQLHENV henv = NULL; SQLHDBC hdbc = NULL; SQLHSTMT hstmt = NULL; SQLRETURN ret; SQLULEN commit_mode; SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); commit_mode = SQL_AUTOCOMMIT_OFF; SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)commit_mode, 0); ret = SQLDriverConnect(hdbc, NULL, "DSN=postgres", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT); if (ret != SQL_SUCCESS) { DumpODBCLog(henv, hdbc, hstmt); exit(1); } SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); ret = SQLExecDirect(hstmt, "INSERT INTO a VALUES(100)", SQL_NTS); if (commit_mode == SQL_AUTOCOMMIT_OFF) { ret = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); } SQLFreeStmt(hstmt, SQL_DROP); SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); return 0; }