MySQL connection problem

 

Hy! I read a lot of article in this topic, but I don't find the solution for my problem. I had a code which is work fine before upgrade MQL4. (My last upgrade was about 1 years ago, so I know a lot of things changed.) But now my MySQL connection doesn't work. I insert the code, and the error messages. If someone can help, please, give me some easy solution. Thanks.

//+------------------------------------------------------------------+
//| Import MySQL functions, varriables                               |
//+------------------------------------------------------------------+

#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 TMSQL, string query, int length);
   void mysql_close(int TMSQL);
#import

   int mysql;
   string host = "host name";
   string user = "user name";
   string password = "password";
   string db = "db name";
   int port = 3306;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   mysql = mysql_init(mysql);
   if(mysql != 0)
      Print("MySQL Allocated!");
   int res = mysql_real_connect(mysql,host,user,password,db,port,NULL,0);
   int err = GetLastError();
   if(res == mysql) 
      Print("MySQL Connected!");
   else 
      Print("Connect Error: ", mysql, " ", mysql_errno(mysql), " ");
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   mysql_close(mysql);
   return;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   int length = 0;
   string query = StringConcatenate("CREATE TABLE trade (id int not null primary key auto_increment, currency varchar(20), timeframe varchar(20), price double, sl double, tp double);");
   Print (query);
   length = StringLen(query);
   mysql_real_query(mysql, query, length);
   int myerr = mysql_errno(mysql);
   if(myerr > 0)
      Print("Create Error: ",myerr);
   else
      Print("MySQL Table Created!");
   
//----
 }
//+------------------------------------------------------------------+


The log file contains the following messages:

2014.04.30 10:50:44.045 Expert MySQL EURUSD,M1: loaded successfully

2014.04.30 10:50:45.938 MySQL EURUSD,M1: MySQL Allocated!

2014.04.30 10:50:48.188 MySQL EURUSD,M1: Connect Error: 121375352 2005

2014.04.30 10:50:48.188 MySQL EURUSD,M1: initialized

2014.04.30 10:50:48.188 MySQL EURUSD,M1: CREATE TABLE trade (id int not null primary key auto_increment, currency varchar(20), timeframe varchar(20), price double, sl double, tp double);

2014.04.30 10:50:48.188 MySQL EURUSD,M1: Create Error: 2006


Thanks,

Attila TÓTH


 

it need to MySQL connection again.

数据库连接断开了,要求重新连接。你重新打开MySQL客户端吧,再试一次,应该就行的了。

 

Guys, any luck. I am having the same problem. Error 2005. This was a program which was working perfectly but now it just can't connect to the DB. Can anyone help?

Atihu - If you have the soln for this, can you please share for the benefit of everyone.


Cheers

 
I would also be interested in a solution to this if anyone has one.  Currently trying to get TickInMySQL.mq4 up and running it can't connect to mySQL
 

From https://www.mql5.com/en/code/11114


"Since build 600 of MT4 was released, it's no longer easy to communicate with DLLs that use ANSI format for strings because strings in MQL4 are now in UNICODE format (read more here)."


libmysql.dll should not work right of the bat anymore, need some string manipulation. More details in code above.

Reason: