2

I wrote c++ class to connect to mysql database:

hpp file

#include <vector>
#include <string>

#include "mysql/mysql.h"

#ifndef _DATA
#define _DATA

class Database {

public:
    string host; 
    string user; 
    string pwd; 
    string db; 
    MYSQL * connection;
    MYSQL_RES *result;
    MYSQL_ROW row;

Database(const string & host,
         const string & user,
     const string & pwd,
     const string & db);

    int createMysqlConnection();
};
#endif

cpp file

#include "Database.hpp"

Database::Database(const string & host,
           const string & user,
           const string & pwd,
           const string & db) :
    mysqlHost(host),
    mysqlUser(user),
    mysqlPassword(pwd),
    mysqlDBName(db)
{}
int Database::createMysqlConnection(){
    MYSQL * connection;
    connection = mysql_init(NULL);
               if(!mysql_real_connect(connection, mysqlHost.c_str(), mysqlUser.c_str(),
               mysqlPassword.c_str(), mysqlDBName.c_str(),
               0, NULL, 0)){
    fprintf(stderr, "Connection to database failed: %s\n",
        mysql_error(connection));
    return EXIT_FAILURE;
    }
    cout << "connected to mysql" << endl;
    };

When I'm trying to access connection variable from the main function or from a different class I always get an error like variable 'connection is not declared in this scope. I tried to use friend classes or inheritance to point to connection variable but it didn't work. I think I'm doing something wrong in my syntax.

Here is an example of how I try to point to this variable from different class:

Class risk: public Database {
public:
vector<sting> parameter;
Datasabse.connection;
etc....
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Begayim Muratalina
  • 621
  • 1
  • 6
  • 12
  • 1
    You should not use, using namespace std in a header file http://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file – ed_me Apr 08 '13 at 07:52
  • you don't need the `Database.hpp` header to be included twice up there. – didierc Apr 08 '13 at 08:04
  • To mark a question as solved please click the check left to the answer that helped you most. Don't update title of the question, please. – fancyPants Apr 10 '13 at 09:18
  • Write an answer rather than editing it into the question please – Lightness Races in Orbit Apr 10 '13 at 09:18
  • Please don't change your question to `Solved`. Instead please post your solution as an answer, which you can later accept. See this [meta SO question](http://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question) for more details – Hasturkun Apr 10 '13 at 09:19
  • ok, i'll do it right now, sorry – Begayim Muratalina Apr 10 '13 at 09:21

2 Answers2

1
MYSQL * connection;

declares the connection variable as local to the constructor only. You must move it to the class definition in order to make it available somehow. Alternatively, make a connect method in your Database object to explicitely create a new connection adlib. You will have to mirror it with a close method.

Yet another option is to create a dedicated class Connection for connections, which would:

  • be constructed with a MYSQL *,
  • be created by the connect method of Database as suggested above; the Database class would act as a Connection factory,
  • sports all the necessary methods to handle it,
  • and would call mysql_close in its destructor, to ensure proper termination of the underlying connection.

For instance:

class Connection {
     protected:
          MYSQL *connection;
    public:
        Connection(MYSQL *c): connection(c){}
        virtual ~Connection() { mysql_close(connection); }
        MYSQL_STMT * prepareQuery(const string &query) { 
             return mysql_prepare(connection, query.c_str(), query.length());
        }
        // other methods
};

The problem with this approach is that you quickly feel compeled to wrap all the API primitives with classes.

didierc
  • 14,572
  • 3
  • 32
  • 52
1

to solve this problem i had to define MYSQL connection in class constructor. here is a code sample:

class1.hpp

***headers***
class Flight { 

public:

Flight(MYSQL * connection);

MYSQL * c;

MYSQL_RES * res;

etc...

Class1.cpp

***headers***

Flight::Flight (MYSQL* connection)

{

c = connection;

};

etc...

main.cpp

***headers***

int main (int argc, char *argv[]) {

Database db(localhost, user, etc..);

db.createMysqlConnection;

Flight fl(db.connection);

fl.GetIDs();

etc...
Begayim Muratalina
  • 621
  • 1
  • 6
  • 12