IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

connection a une BDD MySql


Sujet :

MFC

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut connection a une BDD MySql
    salut !!
    g telechargé Mysql++ mais je sais pas m'en servir : comment on fait pour se connecter a une base de données Mysql ? peut-importe l'hebergeur !

    esuite, ajouter des choses dans la BDD.

    merci
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

  2. #2
    pdl
    pdl est déconnecté
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2002
    Messages : 30
    Points : 33
    Points
    33
    Par défaut
    Tu trouveras tout dans la doc de mysql++, mais voici tout de même un exemple tiré de cette doc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    The following example demonstrates how to open a connection, execute a simple query, and display the results. The code can be found in the file simple1.cc which is located in the examples directory. 
     
    #include <iostream> 
    #include <iomanip> 
    #include <sqlplus.hh> 
     
    int main() { 
      Connection con("mysql_cpp_data"); 
      // The full format for the Connection constructor is 
      // Connection(cchar *db, cchar *host="", 
      //            cchar *user="", cchar *passwd="") 
      // You may need to specify some of them if the database is not on 
      // the local machine or you database username is not the same as your 
      // login name, etc.. 
     
      Query query = con.query(); 
      // This creates a query object that is bound to con. 
     
      query << "select * from stock"; 
      // You can write to the query object like you would any other ostrem 
     
      Result res = query.store(); 
      // Query::store() executes the query and returns the results 
     
      cout << "Query: " << query.preview() << endl; 
      // Query::preview() simply returns a string with the current query 
      // string in it. 
     
      cout << "Records Found: " << res.size() << endl << endl; 
     
      Row row; 
      cout.setf(ios::left); 
      cout << setw(17) << "Item" 
           << setw(4)  << "Num" 
           << setw(7)  << "Weight" 
           << setw(7)  << "Price" 
           << "Date" << endl 
           << endl; 
     
      Result::iterator i; 
      // The Result class has a read-only Random Access Iterator 
      for (i = res.begin(); i != res.end(); i++) { 
        row = *i; 
        cout << setw(17) << row[0] 
             << setw(4)  << row[1] 
             << setw(7)  << row["weight"] 
          // you can use either the index number or column name when 
          // retrieving the colume data as demonstrated above. 
             << setw(7)  << row[3] 
             << row[4] << endl; 
      } 
      return 0; 
    }
    Si cela ne va toujours pas, tu peux utilise ADOLib. Voici aussi un exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    	//Définition du connect string
    	CString sProvider("DSN=mySQL");
    	ADOConnect myConn; // Définition d'un objet de type ADOConnect pour établir la connexion avec la DB
     
    	// Connexion à la base de données
    	if (!myConn.Connect(sProvider)) { 
    		cout << myConn.GetError() << endl; //Affichage de l'erreur en cas de problème
    		exit (0);
    	} else {
    		cout << "connected" << endl; //la connexion à réussie
    		// Selection de la base
    		ADOCommand myCmd(myConn.ADOConn, "use prs");		
    		myCmd.Execute();
    		myCmd =  "update prts set prts_sid = round(prts_sid)";
    		myCmd.Execute();
    		myConn.commit();
    		ADOSelect mySel; //Définitin d'un object ADOSelect pour exécuter une requête
    		//Ouverture du recordset
    		if (!mySel.Open(myConn.ADOConn, "select * from user limit 3000")){
    			cout << mySel.GetError() << endl; // Affichage de l'erreur
    		} else {
    			//Ouverture réussie
    			//Lecture des records jusqu'a ce que EOF soit atteint
    			while (mySel.Fetch()) {
    				//Affichage des colonnes du recordset.
    				for (int i=0; i < mySel.GetColCount(); i++)
    					cout << (LPCSTR)(_bstr_t)mySel.GetField((short)i) << ' ' ;
    				cout << endl;
    			}
    		}
    		//Déconnexion de la db
    		myConn.Disconnect();
    	}
    Bien à toi,

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut merci
    mreci beaucoups !

    j'avoue ne pas avoir compris, dans les ADOLib, comment specifier la clocation de la base de donné : est-ce bien CString sProvider("DSN=mySQL"); ?
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

  4. #4
    pdl
    pdl est déconnecté
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2002
    Messages : 30
    Points : 33
    Points
    33
    Par défaut
    DSN=mySQL est un ConnectString défini via le panneau de configuration ODBC32 ou bien avec myODBC que tu trouves sur le site de mySQL. Ce dernier est très facile à utiliser.

    Bien à toi

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut ok
    Mettons que je veuille me connecter a une BDD MySql sur Kilio , je faitcomment pour dire que c'est sur kilio ?
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut premiers pas
    alors voila, j'ai fait un programme (enfi, juste modifié l'exemple)

    il se compile, se lance, mais il me dit su'il y a une erreur : primo je ne vois ce que c'est (je m'y commais pas trop en SQL...) et secondo, je comprend rien au pogramme.

    tout betement, comment je fais pour me connecter a une BDD ? pour lui envoyer une requete ?

    voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <iostream>
    #include <sys/stat.h> 
    #include <iomanip>
    #include <mysql++>
     
    int main() {
     
    Connection con(use_exceptions); 
      // The full format for the Connection constructor is
     
      // You may need to specify some of them if the database is not on
      // the local machine or you database username is not the same as your
      // login name, etc..
      try {  
    con.real_connect("INI","127.0.0.1","root","",3306,(int)0,60,NULL);  
    		//Connection con("mysql_cpp_data");
    		Query query = con.query();
    		// This creates a query object that is bound to con.
    		query << "INSERT INTO `names` (`Nom`) VALUES ('Moi'); ";
    		// You can write to the query object like you would any other ostrem
     
    		Result res = query.store();
    		// Query::store() executes the query and returns the results
     
    		cout << "Query: " << query.preview() << endl;
    		// Query::preview() simply returns a string with the current query
    		// string in it.
     
    		cout << "Records Found: " << res.size() << endl << endl;
     
    		Row row;
    		cout.setf(ios::left);
    		cout << setw(17) << "Item" 
    			<< setw(4)  << "Num"
    			<< setw(7)  << "Weight"
    			<< setw(7)  << "Price" 
    			<< "Date" << endl
    			<< endl;
     
    		Result::iterator i;
    		// The Result class has a read-only Random Access Iterator
    		for (i = res.begin(); i != res.end(); i++) {
    			row = *i;
    			cout << setw(17) << row[0] 
    				<< setw(4)  << row[1] 
    				<< setw(7)  << row["weight"]
    				// you can use either the index number or column name when
    				// retrieving the colume data as demonstrated above.
    				<< setw(7)  << row[3]
    				<< row[4] << endl;
    		}
      } catch (BadQuery er){ // handle any connection 
                             // or query errors that may come up
        cerr << "Error: " << er.error <<  endl;
        return -1;
     
      } catch (BadConversion er) {
        // we still need to cache bad conversions incase something goes 
        // wrong when the data is converted into stock
        cerr << "Error: Tried to convert \"" << er.data << "\" to a \""
    	 << er.type_name << "\"." << endl;
        return -1;
      }
    	return 0;
    }
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut en fait
    Eh bien ca marche !!!!!!

    bon une petite question :

    j'ai ma requete : "INSERT INTO `names` (`Nom`) VALUES (ouech);"

    ou ouech est une variable char.

    vos aurez devinez que auparavant, j'ai un code qui me definie la valeur de ouech (cin >> ouech). Bon eh bien je sai qu'en SQL, pour utiliser la valeur d'une variable on met %ma_variable mais la ca ne marche pas. donc je vous demande comment ajouter dans la BDD une valeur non-definie avant la compilation masi lors de l'exeution : mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <iostream>
    #include <sys/stat.h> 
    #include <iomanip>
    #include <mysql++>
     
    int main() {
    char ouech[255];
    char requete[1024];
     
    cout << "salut !! Entrez le nom que voulez-vous ajouter dans la base de données :\n"<<endl;
    cin >> ouech;
     
    strcpy(requete,"INSERT INTO `names` (`Nom`) VALUES (ouech);");
    //strcpy(requete,"insert into %5:table values (%q0, %q1, %2, %3, %q4);"); 
     
    Connection con(use_exceptions); 
      // The full format for the Connection constructor is
     
      // You may need to specify some of them if the database is not on
      // the local machine or you database username is not the same as your
      // login name, etc..
      try {  
    con.real_connect("INI","127.0.0.1","root","",3306,(int)0,60,NULL);  
     
    		Query query = con.query();
    		// This creates a query object that is bound to con.
    		query << requete;
    		// You can write to the query object like you would any other ostrem
     
    		Result res = query.store();
    		// Query::store() executes the query and returns the results
    /*
    		cout << "Query: " << query.preview() << endl;
    		// Query::preview() simply returns a string with the current query
    		// string in it.
     
    		cout << "Records Found: " << res.size() << endl << endl;
      
    		Row row;
    				Result::iterator i;
    		// The Result class has a read-only Random Access Iterator
    		for (i = res.begin(); i != res.end(); i++) {
    			row = *i;
    			cout << setw(17) << row[0] 
    				<< setw(4)  << row[1] 
    				<< setw(7)  << row["weight"]
    				// you can use either the index number or column name when
    				// retrieving the colume data as demonstrated above.
    				<< setw(7)  << row[3]
    				<< row[4] << endl;
    		}*/
      } catch (BadQuery er){ // handle any connection 
                             // or query errors that may come up
        cerr << "Error: " << er.error <<  endl;
        return -1;
     
      } catch (BadConversion er) {
        // we still need to cache bad conversions incase something goes 
        // wrong when the data is converted into stock
        cerr << "Error: Tried to convert \"" << er.data << "\" to a \""
    	 << er.type_name << "\"." << endl;
        return -1;
      }
    	return 0;
    }
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    256
    Détails du profil
    Informations personnelles :
    Localisation : Etats-Unis

    Informations forums :
    Inscription : Juin 2002
    Messages : 256
    Points : 121
    Points
    121
    Par défaut decidement
    quand on se creuse les meninges un peut, on arrive vite a nos fin... c'est bon, j'ai resolu mes pb.
    OS : WinXP
    Outils : VC++ 8 (Visual Studio 2005)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. impossible de se connecter sur une BDD MySQL
    Par ikuzar dans le forum Administration système
    Réponses: 0
    Dernier message: 18/08/2010, 12h29
  2. Probleme de connection a une BDD MYSQL
    Par Kookaburra dans le forum ASP
    Réponses: 3
    Dernier message: 18/08/2006, 15h00
  3. Se connecter à une BDD Mysql à partir du c++
    Par crazydede8 dans le forum Bibliothèques
    Réponses: 4
    Dernier message: 06/05/2006, 00h50
  4. [debutant] connection à une BDD MySQL
    Par Golork dans le forum Bases de données
    Réponses: 1
    Dernier message: 11/03/2005, 16h51
  5. [Mysql] Connection delphi à une bdd sous easyphp...
    Par pataluc dans le forum Bases de données
    Réponses: 2
    Dernier message: 14/06/2004, 09h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo