Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2376 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  api mysql c et c++ : problème de std::string

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

api mysql c et c++ : problème de std::string

n°550133
blackgodde​ss
vive le troll !
Posté le 25-10-2003 à 00:31:11  profilanswer
 

bonjour,
 
je cherche à encapsuler l'api mysql c dans des classes c++, et je suis tombé sur un problème qui me paraît étrange :
 
la fonction mysql_connect s'utilise comme ceci en c :
 

Code :
  1. MYSQL demo_db;
  2. if(!mysql_connect(&demo_db, "serveurname", "username", "password" )){
  3.   printf("erreur" );
  4. }


 
j'ai créé une petite classe pour tester :
 

Code :
  1. class myConnection
  2. {
  3.   MYSQL _db;
  4.   string _host;
  5.   string _database;
  6.   string _username;
  7.   string _userpass;
  8. public:
  9.   void Open()
  10.   {
  11. // ceci ne fonctionne pas !!
  12. // le gestionnaire d'erreur est appelé et l'erreur mysql est : can't connect to 'localhost'. (comme si _host == "" alors que host contient le nom du serveur mysql)
  13.     if(!mysql_connect(&_db, _host.c_str(), _username.c_str(), _userpass.c_str()))
  14.     {
  15.        // gestionnaire d'erreur
  16.     }
  17. // alors que ceci fonctionne !!
  18.     string s = _host;
  19.     if(!mysql_connect(&_db, s.c_str(), _username.c_str(), _userpass.c_str()))
  20.     {
  21.        // gestionnaire d'erreur
  22.     }
  23.   }
  24.   void Open(const string & host, const string & database, const string & username, const string & userpass)
  25.   {
  26.     _host = host;
  27.     _database = database;
  28.     _username = username;
  29.     _userpass = userpass;
  30.     Open();
  31.   }
  32. };
  33. int main()
  34. {
  35.   myConnection c;
  36.   c.Open("serveur", "test", "root", "" );
  37. }


 
j'avoue ne pas comprendre ...
qq1 pourrait m'eclairer svp ?


Message édité par blackgoddess le 25-10-2003 à 00:33:17

---------------
-( BlackGoddess )-
mood
Publicité
Posté le 25-10-2003 à 00:31:11  profilanswer
 

n°550138
Kristoph
Posté le 25-10-2003 à 00:42:55  profilanswer
 

Je ne vois pas de problèmes alors je vais mettre le blame de l'erreur sur :
 
- la lib mysql
- le compilo utilisé
- du code "manquant" dans ce post

n°550146
blackgodde​ss
vive le troll !
Posté le 25-10-2003 à 00:50:04  profilanswer
 

la lib utilisé ... c la dll fournie avec le serveur mysql sous windows
 
le compilo : vc++7
 
le code entier ... le voici :
 
mysqlconnection.h

Code :
  1. #ifndef mysqlconnection_h
  2. #define mysqlconnection_h
  3. #define NO_CLIENT_LONG_LONG
  4. #define __WIN__
  5. #pragma comment(lib, "libmySQL.lib" )
  6. #include <mysql.h> // Headers for MySQL usage
  7. namespace mysql
  8. {
  9. class Exception : private exception
  10. {
  11.  string _reason;
  12.  string _host;
  13.  string _database;
  14. public:
  15.  Exception();
  16.  Exception(const string & reason);
  17.  Exception(const string & reason, const string & host, const string & database);
  18.  Exception(MYSQL* db, const string & host, const string & database);
  19.  Exception(const Exception & source);
  20.  const Exception & operator = (const Exception & source);
  21.  const string & Reason();
  22.  const string & Host();
  23.  const string & DataBase();
  24. };
  25. class Connection
  26. {
  27.  MYSQL _db;
  28.  bool _isopen;
  29.  string _host;
  30.  string _database;
  31.  string _username;
  32.  string _userpass;
  33. public:
  34.  Connection();
  35.  Connection(const string & host, const string & database, const string & username, const string & userpass);
  36.  ~Connection();
  37.  void Open();
  38.  void Open(const string & host, const string & database, const string & username, const string & userpass);
  39.  void Close();
  40.  string PrepareString(const string & str);
  41.  unsigned long ExecuteQuery(const string & query, unsigned long* InsertID);
  42.  unsigned long ExecuteQuery(const string & query);
  43. };
  44. }
  45. #endif


 
mysqconnection.cpp

Code :
  1. #include <winsock.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. #include "mysqlconnection.h"
  6. namespace mysql
  7. {
  8. Exception::Exception()
  9. {
  10. }
  11. Exception::Exception(const string & reason)
  12.  : _reason(reason)
  13. {
  14. }
  15. Exception::Exception(const string & reason, const string & host, const string & database)
  16.  : _reason(reason), _host(host), _database(database)
  17. {
  18. }
  19. Exception::Exception(MYSQL* db, const string & host, const string & database)
  20.  : _reason(mysql_error(db)), _host(host), _database(database)
  21. {
  22. }
  23. Exception::Exception(const Exception & source)
  24.  : _reason(source._reason), _host(source._host), _database(source._database)
  25. {
  26. }
  27. const Exception & Exception::operator = (const Exception & source)
  28. {
  29.  _reason = source._reason;
  30.  _host = source._host;
  31.  _database = source._database;
  32.  return *this;
  33. }
  34. const string & Exception::Reason()
  35. {
  36.  return _reason;
  37. }
  38. const string & Exception::Host()
  39. {
  40.  return _host;
  41. }
  42. const string & Exception::DataBase()
  43. {
  44.  return _database;
  45. }
  46. Connection::Connection()
  47.  : _isopen(false)
  48. {
  49. }
  50. Connection::Connection(const string & host, const string & database, const string & username, const string & userpass)
  51.  : _isopen(false), _host(host), _database(database), _username(username), _userpass(userpass)
  52. {
  53. }
  54. Connection::~Connection()
  55. {
  56.  if(_isopen)
  57.   Close();
  58. }
  59. void Connection::Open()
  60. {
  61.  if(_isopen)
  62.   throw Exception("already open", _host, _database);
  63.  if(!mysql_connect(&_db, _host.c_str(), _username.c_str(), _userpass.c_str()))
  64.   throw Exception(&_db, _host, _database);
  65.  if(mysql_select_db(&_db, _database.c_str()))
  66.  {
  67.   mysql_close(&_db);
  68.   throw Exception(&_db, _host, _database);
  69.  }
  70.  _isopen = true;
  71. }
  72. void Connection::Open(const string & host, const string & database, const string & username, const string & userpass)
  73. {
  74.  _host = host;
  75.  _database = database;
  76.  _username = username;
  77.  _userpass = userpass;
  78.  Open();
  79. }
  80. void Connection::Close()
  81. {
  82.  if(_isopen)
  83.  {
  84.   mysql_close(&_db);
  85.  }
  86.  else
  87.   throw Exception("not open" );
  88. }
  89. string Connection::PrepareString(const string & str)
  90. {
  91.  char* encdata = new char[2*str.length() + 1];
  92.  int datasize = mysql_real_escape_string(&_db, encdata, str.c_str(), (unsigned long)str.length());
  93.  string ret(encdata, datasize);
  94.  delete encdata;
  95.  return ret;
  96. }
  97. unsigned long Connection::ExecuteQuery(const string & query)
  98. {
  99.  return ExecuteQuery(query, 0);
  100. }
  101. unsigned long Connection::ExecuteQuery(const string & query, unsigned long* InsertID)
  102. {
  103.  if(!_isopen)
  104.   throw Exception("not open" );
  105.  if(mysql_real_query(&_db, query.c_str(), (unsigned int)query.length()))
  106.   throw Exception(&_db, _host, _database);
  107.  if(InsertID) *InsertID = mysql_insert_id(&_db);
  108.  return mysql_affected_rows(&_db);
  109. }
  110. }


 
main.cpp

Code :
  1. #include <winsock.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. #include "mysqlconnection.h"
  7. int main()
  8. {
  9. mysql::Connection c("serveur", "test", "root", "" );;
  10. try
  11. {
  12.  c.Open();
  13. }
  14. catch(mysql::Exception &e)
  15. {
  16.  cout << e.Reason();
  17. }
  18. }


Message édité par blackgoddess le 25-10-2003 à 00:52:52

---------------
-( BlackGoddess )-
n°550169
Kristoph
Posté le 25-10-2003 à 02:03:27  profilanswer
 

Je ne vois que 2 problèmes :
 
- Il me semble que mysql_select_db renvoie true en cas de succès et donc le test if(mysql_select_db(...)) { thrown Exception } est sans doute inversé.
- Close ne met pas à jour la variable _isopen
 
Sinon, je ne vois vraiment pas le problème. Faut faire une passe au debogueur la.

n°550216
blackgodde​ss
vive le troll !
Posté le 25-10-2003 à 11:12:46  profilanswer
 

au moment ou j'appelle mysql_connect, je regarde avec le debogueur la valeur de _host.c_str(), il me retourne bien "serveur", avec un '\0' a la fin ...


---------------
-( BlackGoddess )-
n°550218
blackgodde​ss
vive le troll !
Posté le 25-10-2003 à 11:23:57  profilanswer
 

pour _isopen de Close, marci beaucoup, c'est corrigé :)
 
pour mysql_select_db, je ne m'etais apparement pas trompé


---------------
-( BlackGoddess )-
n°550226
blackgodde​ss
vive le troll !
Posté le 25-10-2003 à 11:40:47  profilanswer
 

j'utilise une autre api de mysql : mysql_real_connect, qui est plus complète, et qui supporte mon _host.c_str().
 
problème résolu donc :)


---------------
-( BlackGoddess )-

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  api mysql c et c++ : problème de std::string

 

Sujets relatifs
[C++] Je débute : problème de mathstring et char* ?
big problème de retour à la ligneProblème de %5C
[php] problème de logiqueProblème dans une requête SQL
[VB.net] Comment passer un Array String a une fonction sans variableProbleme de Session en Asp (PWS)
[MySQL] sortir la liste des bases accessibles à un user?les nombres à virgule flottante? MySql
Plus de sujets relatifs à : api mysql c et c++ : problème de std::string


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR