1

Estoy tratando de conectarme a una base de datos local MySQL, lo intenté hacer siguiendo: https://github.com/microsoft/VCSamples/blob/master/VC2015Samples/ODBC%20database%20sample%20(linux)/odbcconnector/odbcconnector.c

Estoy utilizando windows, por tal motivo agregué la libreria windows.h

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>

#define RESULT_LEN  256
#define CHECK_ERROR(e, s, h, t) ({\
            if (e!=SQL_SUCCESS && e != SQL_SUCCESS_WITH_INFO) {extract_error(s, h, t); goto exit;} \
})
void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
    SQLINTEGER i = 0;
    SQLINTEGER NativeError;
    SQLCHAR SQLState[7];
    SQLCHAR MessageText[256];
    SQLSMALLINT TextLength;
    SQLRETURN ret;

    fprintf(stderr, "\nThe driver reported the following error %s\n", fn);
    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, SQLState, &NativeError,
            MessageText, sizeof(MessageText), &TextLength);
        if (SQL_SUCCEEDED(ret)) {
            printf("%s:%ld:%ld:%s\n",
                SQLState, (long)i, (long)NativeError, MessageText);
        }
    } while (ret == SQL_SUCCESS);
}
int main() {

    SQLHENV   henv = SQL_NULL_HENV;   // Environment
    SQLHDBC   hdbc = SQL_NULL_HDBC;   // Connection handle
    SQLHSTMT  hstmt = SQL_NULL_HSTMT;  // Statement handle
    SQLRETURN retcode;
    SQLCHAR strResult[RESULT_LEN];
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;
    int i = 0;
    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_ENV)",
        henv, SQL_HANDLE_ENV);
    // Set the ODBC version environment attribute
    retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
        (SQLPOINTER*)SQL_OV_ODBC3, 0);
    CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_ATTR_ODBC_VERSION)",
        henv, SQL_HANDLE_ENV);

    retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
        (SQLPOINTER*)SQL_OV_ODBC3, 0);
    CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_ATTR_ODBC_VERSION)",
        henv, SQL_HANDLE_ENV);
    // Allocate connection handle
    retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)",
        hdbc, SQL_HANDLE_DBC);
    // Set login timeout to 5 seconds
    SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
    CHECK_ERROR(retcode, "SQLSetConnectAttr(SQL_LOGIN_TIMEOUT)",
        hdbc, SQL_HANDLE_DBC);
    // Connect to data source, replace with your connection string
     retcode = SQLDriverConnect(
        hdbc, NULL, "Driver={MySQL ODBC 8.0 ANSI Driver};Server=127.0.0.1;Uid=root;Pwd=xxx;database=dbprueba",//
        SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);

    retcode = SQLDriverConnect(hdbc, NULL, "Driver={MySQL ODBC 8.0 ANSI Driver};Server=127.0.0.1;Uid=root;Pwd=xxx;database=dbprueba", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
    // Allocate statement handle
    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_STMT)",
        hstmt, SQL_HANDLE_STMT);
    retcode = SQLExecDirect(hstmt,
        (SQLCHAR*) "SELECT @@version", SQL_NTS); //DB_NAME()
    CHECK_ERROR(retcode, "SQLExecDirect()", hstmt, SQL_HANDLE_STMT);

    // Bind columns
    retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, &strResult, RESULT_LEN, 0);

    // Fetch and print each row of data until SQL_NO_DATA returned.
    for (i = 0; ; i++) {
        retcode = SQLFetch(hstmt);
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            printf("Result is: %s", strResult);
        }
        else {
            if (retcode != SQL_NO_DATA) {
                CHECK_ERROR(retcode, "SQLFetch()", hstmt, SQL_HANDLE_STMT);
            }
            else {
                break;
            }
        }
    }

exit:
    printf("\nComplete.\n");
    // Free handles
    // Statement
    if (hstmt != SQL_NULL_HSTMT)
        SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    // Connection
    if (hdbc != SQL_NULL_HDBC) {
        SQLDisconnect(hdbc);
        SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    }
    // Environment
    if (henv != SQL_NULL_HENV)
        SQLFreeHandle(SQL_HANDLE_ENV, henv);
    return 0;
}

Desde vs code, le doy a "run code" y obtengo:

C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x83): undefined reference to `SQLGetDiagRec@32'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x11f): undefined reference to `SQLAllocHandle@12'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x177): undefined reference to `SQLSetEnvAttr@16'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x1cf): undefined reference to `SQLSetEnvAttr@16'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x21e): undefined reference to `SQLAllocHandle@12'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x276): undefined reference to `SQLSetConnectAttr@16'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x2ee): undefined reference to `SQLDriverConnect@32'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x33c): undefined reference to `SQLDriverConnect@32'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x35d): undefined reference to `SQLAllocHandle@12'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x3ad): undefined reference to `SQLExecDirect@12'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x417): undefined reference to `SQLBindCol@24'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x430): undefined reference to `SQLFetch@4'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x4bc): undefined reference to `SQLFreeHandle@8'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x4d1): undefined reference to `SQLDisconnect@4'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x4e7): undefined reference to `SQLFreeHandle@8'
C:\Users\Lucas\AppData\Local\Temp\ccaO43tq.o:odbc_connector.c:(.text+0x504): undefined reference to `SQLFreeHandle@8'
collect2.exe: error: ld returned 1 exit status

Traté utilizando makefile y haciendo:

odbc_conn.exe: odbc_connector.c 
    gcc -o odbc_conn odbc_connector.c -lodbc32

al ejecutar desde la terminal :

./odbc_conn

la salida que obtengo es:

The driver reported the following error SQLAllocHandle(SQL_HANDLE_STMT)

Complete.

La cadena de conexión la cree revisando orígenes de datos ODBC (64 bits).

1 respuesta 1

-1
  1. Change the includes in this order: stdio.h, stdlib.h, windows.h, sql.h, sqlext.h 2. Use cl.exe and also review the chain invocation of the methods: CHECK_ERROR, extract_error
1
  • Tal y como está redactada, tu respuesta no es clara. Por favor, pulsa en editar para añadir detalles adicionales que ayuden a los demás a entender cómo esto responde a la pregunta formulada. Puedes encontrar más información sobre cómo escribir buenas respuestas en el centro de ayuda.
    – Comunidad Bot
    Commented el 16 nov. 2023 a las 13:42

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.