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....
}
6
  • 1
    You should not use, using namespace std in a header file stackoverflow.com/questions/14575799/… Commented Apr 8, 2013 at 7:52
  • you don't need the Database.hpp header to be included twice up there. Commented Apr 8, 2013 at 8: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. Commented Apr 10, 2013 at 9:18
  • Write an answer rather than editing it into the question please Commented Apr 10, 2013 at 9: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 for more details Commented Apr 10, 2013 at 9:19

2 Answers 2

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...
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.