MT4-ODBC bridge and some scripts.
Yet another MT4-ODBC bridge, and some scripts.
Most of DBMS provides its ODBC driver, you can access any DBMS from your MQL programs. You can write more complex EAs, useful scripts, etc.
Notice these codes are writtern for my personal use and not intended to develop fully products.
I will maintain and fix bugs if I found or I need, but may not be fixed if I think I don’t need.
Yes you can use these codes by your own risk and rewrite for yourself.
You need Microsoft Visual C++ 2017 Redistributable x86 (https://go.microsoft.com/fwlink/?LinkId=746571)
MT4 is 32bit application, so you need 32bit ODBC/redistributable. NOT x64 even though OS is 64bit.
See \MQL4\Scripts\export.mq4
int OnInit() {
if (!OdbcInitEnv()) {
return (INIT_FAILED);
}
// In the case of SQLite3,
if (!OdbcConnect(dsName, userName, password)) {
return (INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) {
OdbcDisconnect();
OdbcDeInitEnv();
}
string sql = StringConcatenate("insert into usdjpy (time, open, high, low, close) values ('",
dt, "',", open, ",", high, ",", low, ",", close, ")"); // Prepare/placefolder is not supported.
if (!OdbcExecute(sql)) { // result is true/false. if you want last insert ID, use select last_insert_rowid() / last_insert_id() (Depends on DBMS)
Print("Insert failed.");
Print(OdbcErrorCode(), OdbcErrorMsg()); // You can get error reason
}
ulong sth = OdbcQuery("select * from foo"); // You can open more than one cursor
if (sth == 0) {
ERROR
}
OdbcBindColInt(sth); // First column as int
OdbcBindColDouble(sth); // Second column as double
OdbcBindColLong(sth); // 3rd column as long
OdbcBindColString(sth); // 4th column as string
while(OdbcFetch(sth)) {
if (OdbcIsNull(sth, 4)) { // Null check. Column no is base 1. Not base 0
string str = OdbcGetColString(sth, 4); // if the column is null, return "" or 0
}
}
OdbCloseStmt(sth); // Free buffer. Don't forget without you want memory leak.