Visualizzazione dei risultati da 1 a 2 su 2

Discussione: linguaggio c e mysql

  1. #1

    linguaggio c e mysql

    ciao a tutti. qualcuno sa, per favore, indicarmi qualche sito nel quale reperire info su come far interaggire linguaggio c e mysql? insomma le API mysql di C.
    vi ringrazio anticipatamente...
    ciao

  2. #2
    codice:
    /* bisogna linkare la libreria: "odbc32.lib" (in Dev C++ "libodbc32.a") */
    
    #include <windows.h>       
    #include <sqlext.h>                                           // interfaccia ODBC con il Database
    #include <stdio.h>         
    
    HENV     hEnv = NULL;                                         // Env Handle from SQLAllocEnv()
    HDBC     hDBC = NULL;                                         // Connection handle
    HSTMT    hStmt = NULL;                                        // Statement handle
    UCHAR    szDSN[SQL_MAX_DSN_LENGTH] = "ACCESSstandardCwin32";  // Data Source Name buffer "ACCESSstandardCwin32"
    UCHAR*   szUID = NULL;                                        // User ID buffer 
    UCHAR*   szPasswd = NULL;                                     // Password buffer
    UCHAR    lastname[50];                                        // Model buffer field1
    UCHAR    firstname[50];                                       // Model buffer field2
    UCHAR    telephone[50];                                       // Model buffer field3
    SDWORD   cbModel;                                             // Model buffer bytes recieved
    UCHAR    szSqlStr[] = "SELECT * FROM mia_tabella;";           // SQL string
    RETCODE  retcode;                                             // Return code
    
    int main()
    {
        // Allocate memory for ODBC Environment handle
        SQLAllocEnv (&hEnv);
    
        // Allocate memory for the connection handle
        SQLAllocConnect (hEnv, &hDBC);
    
        // Connect to the data source "ACCESSdevcpp" 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);                
            printf ("Operation SELECT success\n"); // notify operation
    
            // Project only column 1 which is the models
            SQLBindCol (hStmt, 1, SQL_C_CHAR, lastname, sizeof(lastname), &cbModel);
            // Project only column 2 which is the models
            SQLBindCol (hStmt, 2, SQL_C_CHAR, firstname, sizeof(firstname), &cbModel);
            // Project only column 3 which is the models
            SQLBindCol (hStmt, 3, SQL_C_CHAR, telephone, sizeof(telephone), &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 ("lastname  : %s\n", lastname);  // Print row (model)
                printf ("firstname : %s\n", firstname); // Print row (model)
                printf ("telephone : %s\n", telephone); // 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);
    
        system("pause");
        return 0;
    
    }
    standard per windows... funziona con "quasi" tutti i compilatori C/C++
    (Dev C++, Microsoft VC++, Borland C++)
    e permette di collegarsi a "quasi" tutti i database relazionali (Access, MySql, Sql Server, Oracle).

    p.s. basta creare un DSN di sistema con il database interessato.

    N.B. Senza DSN di sistema: QUA
    ...Terrible warlords, good warlords, and an english song

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.