Problem mit MySQL connection(Connection aufbauen beenden und wieder connecten)

FBIagent

Erfahrenes Mitglied
Moin,

wie im Betreff gesagt connecte ich zu meinem MySQL Server(localhost).
Für die MySQL connection habe ich mir eine ganz kleine Klasse geschrieben.

Wenn die connection beendet wird und wieder aufgebaut werden soll funktioniert es
nicht mehr. Wäre nett wenn mir jemand helfen könnte den Fehler zu finden ich sehe da überhaupt nichts falsches.

Hier etwas code:
Code:
//// mysqlConnection.h ////
#ifndef MYSQLCONNECTION_H
#define MYSQLCONNECTION_H
 
#include <iostream>
#include <windows.h>
#include <MySQL\mysql.h>
 
class mysqlConnection
{
private:
  MYSQL conn;
  std::string info[4];
public:
  void setInfo(std::string host,std::string user,std::string password, std::string database);
  bool connect();
  bool query(std::string data);
  MYSQL_RES *lastRes();
  void resFree(MYSQL_RES *data);
  void close();
};
#endif
 
//// mysqlConnection.cpp ////
#include "mysqlConnection.h"
 
void mysqlConnection::setInfo(std::string host,std::string user,std::string password,std::string database)
{
  info[0] = host;
  info[1] = user;
  info[2] = password;
  info[3] = database;
}
 
bool mysqlConnection::connect()
{
  mysql_init(&conn);
 
  if(mysql_real_connect(&conn,info[0].c_str(),info[1].c_str(),info[2].c_str(),info[3].c_str(),3306,NULL,0))
    return true;
  else
    return false;
}
 
bool mysqlConnection::query(std::string data)
{
  if(!mysql_query(&conn,data.c_str()))
    return false;
  else
    return true;
}
 
MYSQL_RES *mysqlConnection::lastRes()
{
  return mysql_store_result(&conn);
}
 
void mysqlConnection::resFree(MYSQL_RES *data)
{
  mysql_free_result(data);
}
 
void mysqlConnection::close()
{
  mysql_close(&conn);
}
 
Du solltest vielleicht mal zeigen wie Du Deine Klasse benutzt und wie Du den Reconnect machst.
 
Zurück