1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   |  
int TForm1::Total_Vehicule(void)
{
    MYSQL* mySQL = NULL;
 
    /** Obtenir un descripteur d'accès au SGBD */
    if((mySQL = mysql_init(NULL)) == NULL)
        return -1;
 
    /** Se connecter à la BDD */
    if ((mysql_real_connect(mySQL, "127.0.0.1", "root", "root", "laba", 0, NULL, 0)) == NULL)
    {
        MessageDlg("Erreur de connexion à la base de données !",  mtError, TMsgDlgButtons() << mbOK, 0);
        return -1;
    }
 
    int nbre_voiture;
    mysql_query (mySQL, "SELECT * FROM voiture");
    MYSQL_RES *myRES = mysql_store_result (mySQL);
    nbre_voiture = mysql_num_rows(myRES);
    return nbre_voiture;
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ShowMessage(Total_Vehicule());
}
//---------------------------------------------------------------------------
 
 
void TForm1::Requete_ecrire(char immatricule[15],char dateGrise[15],char dateService[15])
{
    MYSQL* mySQL = NULL;
 
    /** Obtenir un descripteur d'accès au SGBD */
    if((mySQL = mysql_init(NULL)) == NULL)
        return ;
 
    /** Se connecter à la BDD */
    if ((mysql_real_connect(mySQL, "127.0.0.1", "root", "root", "mabase", 0, NULL, 0)) == NULL)
    {
        MessageDlg("Erreur de connexion à la base de données !",  mtError, TMsgDlgButtons() << mbOK, 0);
        return ;
    }
 
    char *reqSQL= new char [100]; // Taille de la requête.
 
    sprintf (reqSQL, "SELECT * FROM voiture WHERE imma = '%s'", immatricule);
    mysql_query (mySQL, reqSQL);
    MYSQL_RES *myRES = mysql_store_result (mySQL);
    if(mysql_num_rows(myRES) != 0)
    {
        MessageBox(NULL,"Le véhicule éxiste déjà.","Attention: Véhicule éxistant.",MB_OK |MB_ICONEXCLAMATION);
    }
    else
    {
        sprintf(reqSQL,"INSERT INTO voiture (imma,date,date1) VALUES ('%s','%s','%s')",immatricule,dateGrise,dateService);
        if (mysql_query (mySQL, reqSQL) != 0)
                MessageBox(NULL,"Impossible d'enregistrer un nouveau véhicule.","Erreur: Ecriture dans la BDD.",MB_OK |MB_ICONERROR);
    }
    delete reqSQL;
}
//--------------------------------------------------------------------------
 
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    Requete_ecrire(Edit1->Text.c_str(), Edit2->Text.c_str(), Edit3->Text.c_str());
}
//--------------------------------------------------------------------------- | 
Partager