Connecting to a MySQL database

 

Hello, I'm trying to connect to a MySQL but something seems to be wrong, I can't figure out what .. This is the code I'm using, the connection result keeps on returning 0  (user and pass variables are modified here ), I can connect to the DB without problem by other means, anything particularly wrong here ?



#import "libmysql.dll"
   int mysql_init(int db);
   int mysql_errno(int TMYSQL);
   int mysql_real_connect(int TMYSQL, string& host, string& user, string& password,
                           string& DB,int port,int socket,int clientflag);
   int mysql_real_query(int TMYSQL,string& query,int lenght);
   void mysql_close(int TMSQL);                       
   string mysql_error(int TMYSQL); //string is ansi
#import
string MySqlHost   ="localhost"; //MySql Host:
string MySqlUser   ="user";     //MySQL User:
string MySqlPass   ="pass";      //MySQL Password:
string MySqlDB     ="forex2";     //MySQL Table:
int    MySqlPort   =3306;        //MySQL Port:
string MySqlSocket ="";          //MySQL Socket:
input int    MySqlFlag   =0;           //MySQL Flag:
int mysql   =0;
string query="";
MqlTick tick;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 
     string host,user,pass,DB;
   if(MQL5InfoInteger(MQL5_DLLS_ALLOWED)==0)
      {
        Alert("DLL calling not allowed. Allow and try again!");
      }
   mysql=mysql_init(0);
   Print("obiectul mysql=",mysql);
   host=MySqlHost;
   user=MySqlUser;
   pass=MySqlPass;
   DB=MySqlDB;
   int res=mysql_real_connect(mysql,host,user,pass,DB,MySqlPort,MySqlSocket,MySqlFlag);
   Print("connection result=",res);
//---
  
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }
//+------------------------------------------------------------------+

 

the solution is in the following article:
http://mqlmagazine.com/mql-programming/mql5-connecting-to-mysql/
particularly, you have to use the include file <stringlib.mqh> for correctly passing parameters to mysql dll.
you may find the code for stringlib.mqh here:
http://mqlmagazine.com/mql-programming/dll-hell-mql5-edition-unicode-vs-ansi/ 

MQL5 : Connecting to MySQL | MQLmagazine.com
  • mqlmagazine.com
This article is dedicated to sending data from MQL5 to a MySQL database. The article is at the same time an example for the functions presented in the article DLL Hell, MQL5 Edition : UNICODE vs ANSI. The main advantages of collecting data in MySQL databases would be : data interrogration, lower space needed for important quantities of data...