Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [C++] Database

  1. #1

    [C++] Database

    Ciao a tutti! sono sempre io...almeno a qst rispondete se potete...mi pare strano nn sappiate qst cose (visto ke ho guardato agli altri post e c sono cose ben + complicate).
    E' possibile creare 1 programma in C++ ke gestisca un database di access?
    Tutto senza WinAPI bensi sempre in DOS.

    PLZ RISPONDETE! GRAZIE E CIAUZ!
    Alessio_Programmer

  2. #2
    senza DSN di sistema
    codice:
    /* bisogna linkare la libreria: (con DevC++) "libodbc32.a" con gli altri compilatori "odbc32.lib" "*/
    
    #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] = "ACCESSdevc";    // Data Source Name buffer "ACCESSdevc"
    UCHAR    szDSNless[] = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=mio_database.mdb";
    UCHAR*   szUID = NULL;                                // User ID buffer 
    UCHAR*   szPasswd = NULL;                             // Password buffer
    UCHAR    szModelname[50];                             // Model buffer field1
    UCHAR    szModelfirstname[50];                        // Model buffer field2
    UCHAR    szModeltelephone[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 "ACCESSdevc" using DSN.
        //retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
    
        // Connect to the data source "ACCESS database" using DSN-less.
        retcode = SQLDriverConnect(hDBC, NULL, (SQLCHAR *)szDSNless, 
            SQL_NTS, (SQLCHAR *)szDSNless, sizeof(szDSNless), NULL, SQL_DRIVER_NOPROMPT);
    
        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, szModelname, sizeof(szModelname), &cbModel);
            // Project only column 2 which is the models
            SQLBindCol (hStmt, 2, SQL_C_CHAR, szModelfirstname, sizeof(szModelfirstname), &cbModel);
            // Project only column 3 which is the models
            SQLBindCol (hStmt, 3, SQL_C_CHAR, szModeltelephone, sizeof(szModeltelephone), &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 ("%s ", szModelname);        // Print row (model)
                printf ("%s ", szModelfirstname);   // Print row (model)
                printf ("%s\n", szModeltelephone);  // 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;
    }
    con DSN di sistema
    http://forum.html.it/forum/showthrea...hreadid=669954

    altro (solo per Microsoft C++)
    http://forum.html.it/forum/showthrea...62#post2943162
    ...Terrible warlords, good warlords, and an english song

  3. #3
    Ciao! ti ringrazio tantissimo per la risposta.
    Solo ke io sono popo noobo e del C++ so programmare bene solo le cose basi (ho studiato fino alle classi) e quindi non ho mai utilizzato altre librerie ke contengono funzioni ( a me molto sconosciute)
    quindi se conosci proprio 1 guida (magari stampabile) per partire dalle basi te ne sarei veramente grato!
    CIAO!
    Alessio_Programmer

  4. #4
    mi permetto di fare un UP a questo interessante topic, anche se ormai ben datato.

    ho provato ad eseguire tale codice per accedere ad un db chiamato db.mdb e posizionato in C:\
    Però qualsiasi cosa faccia di questo programma viene eseguito solamente il "system("pause");" finale, il resto sembra essere saltato "a piè pari"

    ho provato a mettere un pò di printf in giro, anche subito prima del system(pause) ma non vengono eseguiti.
    Come faccio a far funzionare questo script?
    Inoltre, dove devo mettere la locazione del mio db per farglielo riconoscere?
    Grazie in anticipo!

  5. #5
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,314

    Moderazione

    Originariamente inviato da AlexanderPD
    mi permetto di fare un UP a questo interessante topic, anche se ormai ben datato.
    E' una pratica da evitare, come indicato nel Regolamento, visto che peraltro il contenuto di questa discussione non aggiunge nulla che tu possa sfruttare per descrivere il tuo problema specifico.

    Apri una discussione nuova e separata, con titolo significativo e linguaggio, per discutere del tuo problema.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

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 © 2024 vBulletin Solutions, Inc. All rights reserved.