Use libmysql.dll directly to read query result from MYSQL

 

Since mql4 doesn't support point, I didn't find any way to read the result of a select sentence. Maybe someone already created some dlls to do that which I don't know.

Here is my way, use concat_ws to put all the returned columns into one string. Map the returned row structure to a string in mql4. Then scan the string and convert to expected column values.

 
//+------------------------------------------------------------------+

//| mySQLTester.mq4 |

//| Copyright ?2006, GP2X |

//| |

//+------------------------------------------------------------------+

#property copyright "Copyright ?2006, GP2X"

#property link ""

#define DELIM ";"

//#include

#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);

int mysql_store_result(int TMSQL);

string mysql_fetch_row(int result);

int mysql_num_rows(int result);

void mysql_free_result(int result);

#import

int mysql;

int mTicket, mType;

string mSymbol;

double mLots, mOpen, mClose, mStopLoss, mTakeProfit;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

string row;

connect();

string query="Select concat(';',concat_ws(';', ticket, symbol, type, lots, open, close, stoploss, takeprofit)) from trades";

int length=StringLen(query);

mysql_real_query(mysql,query,length);

int result = mysql_store_result(mysql);

int numOfRows = mysql_num_rows(result);

for (int i=0;i<numOfRows;i++) {

row = mysql_fetch_row(result);

decodeTrade(row);

Comment("Ticket=", mTicket, ",Symbol=", mSymbol, ",Type=", mType, ",Lots=", mLots, ",Open=", mOpen, ",Close=", mClose, ",SL=", mStopLoss, ",TakeProfit=", mTakeProfit);

}

mysql_free_result(result);

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

mysql_close(mysql);

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

void connect() {

mysql = mysql_init(mysql);

if (mysql!=0) Print("allocated");

string host="localhost";

string user="GP2X";

string password="forex";

string DB="forex";

int clientflag=0;

int port=3306;

string socket="";

int res=mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);

int err=GetLastError();

if (res==mysql) Print("connected");

else Print("error=",mysql," ",mysql_errno(mysql)," ");

}

void decodeTrade(string trade) {

int begin = StringFind(trade, DELIM)+1;

int end = StringFind(trade, DELIM, begin);

mTicket = StrToInteger(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mSymbol = StringSubstr(trade, begin, end-begin);

begin = end+1;

end = StringFind(trade, DELIM, begin);

mType = StrToInteger(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mLots = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mOpen = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mClose = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mStopLoss = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringLen(trade);

mTakeProfit = StrToDouble(StringSubstr(trade, begin, end-begin));

}
 

connect to oracle XE from Metatrader

Has anyone tried it? I can connect to MySQL but have no idea how to do it with Oracle?

 

it work fine

it work fine

GP2X:
//+------------------------------------------------------------------+

//| mySQLTester.mq4 |

//| Copyright ?2006, GP2X |

//| |

//+------------------------------------------------------------------+

#property copyright "Copyright ?2006, GP2X"

#property link ""

#define DELIM ";"

//#include

#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);

int mysql_store_result(int TMSQL);

string mysql_fetch_row(int result);

int mysql_num_rows(int result);

void mysql_free_result(int result);

#import

int mysql;

int mTicket, mType;

string mSymbol;

double mLots, mOpen, mClose, mStopLoss, mTakeProfit;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

string row;

connect();

string query="Select concat(';',concat_ws(';', ticket, symbol, type, lots, open, close, stoploss, takeprofit)) from trades";

int length=StringLen(query);

mysql_real_query(mysql,query,length);

int result = mysql_store_result(mysql);

int numOfRows = mysql_num_rows(result);

for (int i=0;i<numOfRows;i++) {

row = mysql_fetch_row(result);

decodeTrade(row);

Comment("Ticket=", mTicket, ",Symbol=", mSymbol, ",Type=", mType, ",Lots=", mLots, ",Open=", mOpen, ",Close=", mClose, ",SL=", mStopLoss, ",TakeProfit=", mTakeProfit);

}

mysql_free_result(result);

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

mysql_close(mysql);

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

void connect() {

mysql = mysql_init(mysql);

if (mysql!=0) Print("allocated");

string host="localhost";

string user="GP2X";

string password="forex";

string DB="forex";

int clientflag=0;

int port=3306;

string socket="";

int res=mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);

int err=GetLastError();

if (res==mysql) Print("connected");

else Print("error=",mysql," ",mysql_errno(mysql)," ");

}

void decodeTrade(string trade) {

int begin = StringFind(trade, DELIM)+1;

int end = StringFind(trade, DELIM, begin);

mTicket = StrToInteger(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mSymbol = StringSubstr(trade, begin, end-begin);

begin = end+1;

end = StringFind(trade, DELIM, begin);

mType = StrToInteger(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mLots = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mOpen = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mClose = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringFind(trade, DELIM, begin);

mStopLoss = StrToDouble(StringSubstr(trade, begin, end-begin));

begin = end+1;

end = StringLen(trade);

mTakeProfit = StrToDouble(StringSubstr(trade, begin, end-begin));

}
 

"libmysql.dll" does not work

Dear All, I am using "libmysql.dll" in my EA, on testing mode, this seems to work fine, and in live mode Metatrader crash.

Anyone have any idea how to solve this ?

 

I'd suggest that you check if you have the same, or only one, libmysql.dll file.

And, that this is the same DLL file that came with your version of MySQL.

Lee

 

Hello,

interesting, but it looks like incomplete or not working !

 

How to use update query to update data in mysql from mql4 ?

muyuan11:
it work fine

How to use update query to update data in mysql from mql4 ?