Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
you don't need to program a DLL, you can use existing.
for example MySQL:
Code:
#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;
int init(){
//----
mysql=mysql_init(mysql);
if (mysql!=0) Print("allocated");
string host="localhost";
string user="user";
string password="pwd";
string DB="mt4";
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)," ");
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
mysql_close(mysql);
//----
return(0);
}
int start()
{
string query="";
int length=0;
query=StringConcatenate("insert into ticks(margin,freemargin,date,ask,bid,symbol,equity) values(",AccountMargin(),",",AccountFreeMargin(),",\"",TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS),"\",",NormalizeDouble(Ask,4),",",NormalizeDouble(Bid,4),",\"",Symbol(),"\",",AccountEquity(),");");
length=StringLen(query);
mysql_real_query(mysql,query,length);
int myerr=mysql_errno(mysql);
if (myerr>0)Print("error=",myerr);
}
hope this helps.
read also MySQL documentation for DLL usage.
for libmysql.dll look in "lib" subfolder of your mysql installation.
there is one problem though as metatrader can get only scalar datatypes from DLLs.
DDL and DML SQL statements work just fine, but if you need to read something from database with SELECT, it is not so trivial as result is in memory as some structure, which can not be read in MQ4.
to override this I had to output query result to a file and then read it in MQ4:
Code:
int GetClientTrades() {
FileDelete(queryFile);
string query="SELECT * INTO OUTFILE \'"+queryPFile+"\' FIELDS TERMINATED BY \';\' LINES TERMINATED BY \'\n\'FROM clients_trades;";
Print(query);
int length=StringLen(query);
mysql_real_query(mysql,query,length);
int myerr=mysql_errno(mysql);
if (myerr>0) Print("error=",myerr);
int handle,res,size,i;
ArrayResize(clientTrades,0);
handle=FileOpen(queryFile,FILE_CSV|FILE_READ);
if(handle<1) {
Print("File " + queryFile + " not found, the last error is ", GetLastError());
return(0);
}
while (!FileIsEnding(handle)) {
size++;
ArrayResize(clientTrades,size);
clientTrades[size-1][0]=FileReadNumber(handle);
clientTrades[size-1][1]=FileReadNumber(handle);
clientTrades[size-1][2]=FileReadNumber(handle);
clientTrades[size-1][3]=FileReadNumber(handle);
clientTrades[size-1][4]=FileReadNumber(handle);
clientTrades[size-1][5]=FileReadNumber(handle);
clientTrades[size-1][6]=FileReadNumber(handle);
clientTrades[size-1][7]=FileReadNumber(handle);
clientTrades[size-1][8]=FileReadNumber(handle);
clientTrades[size-1][9]=FileReadNumber(handle);
}
ArrayResize(clientTrades,size-1);
size--;
//if (clientTrades[size-1][0]==0.0) ArrayResize(clientTrades,size-1);
FileClose(handle);
return(1);
}
sorry to bring up an older thread but I have successfully connected and inserted data into a mysql database via the EA but I can't seem to figure out your code to SELECT data. I copied the function but it came up with a lot of errors.