Pergunta

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....
}
Foi útil?

Solução

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...

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top