Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Permite a ligação ODBC a uma BD */
- // Apple Xcode
- // gcc -lodbc main.c -o teste
- #include <stdio.h>
- #include <sqlext.h>
- int main (int argc, const char * argv[])
- {
- // environment handle
- HENV the_henv = NULL;
- // connection handle
- HDBC the_hdbc = NULL;
- // statement handle
- HSTMT the_hstmt = NULL;
- // odbc
- UCHAR the_dsn[SQL_MAX_DSN_LENGTH] = "ODBC_CONNECTION_NAME";
- UCHAR* the_uid = NULL; // (unsigned char *) "login";
- UCHAR* the_password = NULL; // (unsigned char *) "password";
- // field var
- SDWORD the_cell_buffer; // buffer
- UCHAR the_column_a[255];
- UCHAR the_column_b[255];
- UCHAR the_column_c[255];
- // sql query
- UCHAR the_sql_string[] = "SELECT column_a, column_b, column_c FROM table";
- // SQLRETURN
- RETCODE the_retcode;
- // allocates an environment handle and associated resources
- SQLAllocEnv(&the_henv);
- // allocates a connection handle and associated resources within the environment identified by the input environment handle
- SQLAllocConnect(the_henv, &the_hdbc);
- // establishes connections to a driver and a data source
- the_retcode = SQLConnect(the_hdbc, the_dsn, SQL_NTS, the_uid, SQL_NTS, the_password, SQL_NTS);
- // after connection...
- if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
- {
- // allocates a new statement handle and associates it with the connection specified by the connection handle
- the_retcode = SQLAllocStmt(the_hdbc, &the_hstmt);
- // associates an SQL statement with the input statement handle provided
- the_retcode = SQLPrepare(the_hstmt, the_sql_string, sizeof(the_sql_string));
- // executes a statement that was successfully prepared using SQLPrepare() on the same statement handle
- the_retcode = SQLExecute(the_hstmt);
- if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
- {
- while (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
- {
- // binds application data buffers to columns in the result set
- SQLBindCol(the_hstmt, 1, SQL_C_CHAR, the_column_a, sizeof(the_column_a), &the_cell_buffer);
- SQLBindCol(the_hstmt, 2, SQL_C_CHAR, the_column_b, sizeof(the_column_b), &the_cell_buffer);
- SQLBindCol(the_hstmt, 3, SQL_C_CHAR, the_column_c, sizeof(the_column_c), &the_cell_buffer);
- the_retcode = SQLFetch(the_hstmt);
- if (the_retcode != SQL_NO_DATA_FOUND)
- {
- printf("%s %s %s\n", the_column_a, the_column_b, the_column_c);
- }
- else
- {
- printf("...");
- }
- }
- }
- else
- {
- printf("erro: na construcao da consulta sql\n");
- }
- // process data
- if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
- {
- SQLCancel(the_hstmt);
- SQLFreeHandle(SQL_HANDLE_STMT, the_hstmt);
- }
- // free
- SQLFreeStmt(the_hstmt, SQL_DROP);
- SQLDisconnect(the_hdbc);
- }
- else
- {
- printf("erro: sem sucesso\n");
- }
- // free hEnv
- SQLFreeEnv(the_henv);
- // sleep windows (Sleep): ms; posix (sleep): seconds
- sleep(5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement