c++ & ODBC *newbie*



  • Hallo zusammen !

    Ich bin gerade erst in c++ eigestiegen und möchte mit C++ & ODBC eine
    Verbindung (und später Abfragen) zu einer mySQL-Datenbank realisieren.
    Ich habe im Internet dazu auch schon Beispielcode gefunden.
    Aber wenn ich versuche den Code zu compilieren (ich benutze Dev-C++)
    wirft mir der Compiler folgende Fehlermeldung(en) raus:

    [Linker error] undefined reference to 'SQLAllocEnv@4'
    [Linker error] undefined reference to 'SQLAllocConnect@8'
    [Linker error] undefined reference to 'SQLConnect@28'
    [Linker error] undefined reference to 'SQLAllocStmt@8'
    [Linker error] undefined reference to 'SQLPrepare@12'

    ... und noch viele weitere dieser Art.
    Hier ist der Quellcode dazu:

    // This program connects to an ODBC data source called "test". It then executes //a SQL statement “SELECT Model

    // FROM Makes WHERE Make = 'Vauxhall';" which should return all models made by //Vauxhall stored in the database
    // Author: Mark Lee-Dell Date: 28/05/99 Name: sqlexecute.cpp

    #include <windows.h>
    #include <sqlext.h>
    #include <stdio.h>
    #include "C:\Programme\mysql\include\mysql.h"

    int main(void)

    {
    HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
    HDBC hDBC = NULL; // Connection handle
    HSTMT hStmt = NULL; // Statement handle
    UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "test"; // Data Source Name buffer
    UCHAR* szUID = NULL; // User ID buffer

    UCHAR* szPasswd = NULL; // Password buffer
    UCHAR szModel[128]; // Model buffer
    SDWORD cbModel; // Model buffer bytes recieved
    UCHAR szSqlStr[] = "Select Model From Makes Where Make='Vauxhall'"; // SQL string
    RETCODE retcode; // Return code

    // Allocate memory for ODBC Environment handle
    SQLAllocEnv (&hEnv);

    // Allocate memory for the connection handle
    SQLAllocConnect (hEnv, &hDBC);

    // Connect to the data source "test" using userid and password.
    retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);

    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    {
    // Allocate memory for the statement handle
    retcode = SQLAllocStmt (hDBC, &hStmt);

    // Prepare the SQL statement by assigning it to the statement handle
    retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));

    // Execute the SQL statement handle
    retcode = SQLExecute (hStmt);

    // Project only column 1 which is the models
    SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);

    // Get row of data from the result set defined above in the statement
    retcode = SQLFetch (hStmt);

    while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    {
    printf ("\t%s\n", szModel); // Print row (model)
    retcode = SQLFetch (hStmt); // Fetch next row from result set
    }

    // Free the allocated statement handle
    SQLFreeStmt (hStmt, SQL_DROP);

    // Disconnect from datasource
    SQLDisconnect (hDBC);
    }

    // Free the allocated connection handle
    SQLFreeConnect (hDBC);

    // Free the allocated ODBC environment handle
    SQLFreeEnv (hEnv);
    return 0;
    }

    Ich wäre happy, wenn mir jemand dabe helfen könnte.
    Danke schnmal im Voraus!

    - Thomas -



  • wieso hast du mysql mit drin wenn du odbc machst?
    willst du verbinden?

    hmm

    du musst zwei libs mitlinken:
    odbc32.lib odbccp32.lib

    teste mal ein ganz einfaches beispiel, datenbank vorher anlegen..

    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sql.h>
    #include <sqlext.h>
    
    #define SMALLER_FIRST 1		
    
    void main(void)
    {
    
    	SQLHANDLE henv;
    	SQLHANDLE hdbc;
    	SQLHANDLE hstmt;
    	RETCODE rc;
    
    	char Statement[50] = {"select * from adressentbl"};		
    	char dsn[10] = {"elise"};
    	char user[5] = {""};
    	char pass[5] = {""};
    
    	char m_titleid[10] = {"\0"};
    	char param2[22] = {"The Gourmet Microwave"};
    	char param1[14] = {"Net Etiquette"};
    	param1[13] = '\0';
    	param2[21] = '\0';
    
    	long sqlnts = SQL_NTS;
    	long m_bytesRet = SQL_NTS;
    
    	rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    
    	rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, 
    			(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
    
    	rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    
    	rc = SQLConnect(hdbc, (SQLCHAR *)dsn, SQL_NTS, (SQLCHAR *) user, SQL_NTS, 
    			(SQLCHAR *) pass, SQL_NTS);
    
    	rc = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
    
    	rc = SQLPrepare(hstmt, (SQLCHAR *)Statement, SQL_NTS);
    
    #if SMALLER_FIRST	//Put smaller parameter first.
    	rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_WCHAR, 13, 0,
    			(SQLCHAR*)param1, 13, &sqlnts);	
    #else
    	rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_WCHAR, 21, 0,
    			(SQLCHAR*)param2, 21, &sqlnts);	
    #endif
    
    	rc = SQLExecute(hstmt);
    
    	rc = SQLFetch(hstmt);
    
    	if (rc == SQL_NO_DATA_FOUND)
    	{
    		printf("Parameter #1 (%s) returned no data.\n", param1);
    		SQLFreeStmt(hstmt, SQL_DROP);
    		SQLDisconnect(hdbc);
    		SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    		SQLFreeHandle(SQL_HANDLE_ENV, henv);
    		exit(0);
    	}
    	else
    	{
    		SQLGetData(hstmt, 1, SQL_C_CHAR, m_titleid, 10, &m_bytesRet);
    		printf("Title_id for parameter (%s) = %s\n", SMALLER_FIRST?param1:param2, m_titleid);
    	}
    
    	rc = SQLFreeStmt(hstmt, SQL_CLOSE);
    
    	memset(m_titleid,0,10);
    
    #if SMALLER_FIRST	//Put larger parameter last.
    	rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_WCHAR, 21, 0,
    			(SQLCHAR*)param2, 21, &sqlnts);	
    #else
    	rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_WCHAR, 13, 0,
    			(SQLCHAR*)param1, 13, &sqlnts);	
    #endif
    
    	rc = SQLExecute(hstmt);
    
    	rc = SQLFetch(hstmt);
    
    	if (rc == SQL_NO_DATA_FOUND)
    	{
    		printf("Parameter #2 (%s) returned no data.\n", param2);
    		SQLFreeStmt(hstmt, SQL_DROP);
    		SQLDisconnect(hdbc);
    		SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    		SQLFreeHandle(SQL_HANDLE_ENV, henv);
    		exit(0);
    	}
    	else
    	{
    		SQLGetData(hstmt, 1, SQL_C_CHAR, m_titleid, 10, &m_bytesRet);
    		printf("Title_id for parameter (%s) = %s\n", SMALLER_FIRST?param2:param1, m_titleid);
    	}
    
    	rc = SQLFreeStmt(hstmt, SQL_CLOSE);
    	rc = SQLDisconnect(hdbc);
    	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    	SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }
    


  • Danke für Deine Antwort !

    Ich habe "mysql.h" rausgeschmissen und dafür die beiden LIBs in mein
    Projekt eingebunden. Der Compiler schmeißt daraufhin zig andere
    Fehlermeldungen raus (betreffend Fehler im make-File). Das Gleiche
    auch bei dem Code-Beispiel, welches Du mir bereitgestellt hast.

    Ich befürchte, ich bin einfach zu beschränkt um die IDE (Dev-C++) gescheit
    zu nutzen. Vielleicht versuche ich's erstmal mit etwas einfacheren Dingen.

    Vielen Dank nochmal für Deine Hilfe!
    - Thomas -



  • es gibt eine c und eine c++ api, die mysql zur verfügung stellt.

    solltest du dich perspektivisch für eine mysql datenbank entscheiden, schau doch auf deren seite nach der geeigneten api.



  • Ich habe nun doch mal eine andere IDE bei mir installiert und siehe da:
    Es funzt 🤡

    Ich habe mir bei www.mysql.com die Treiber (mvSQL++) runtergeladen.
    Es ist alles noch ein bisserl schwierig, für einen Anfänger.
    Aber nun bin ich guter Dinge.

    Ich danke Dir für Deine Hilfe!
    Gruß - Thomas -


Anmelden zum Antworten