Problems with MySQL

 

I have working code for access to MySQL in MT5/MQL5. It uses the modules ANSI2UNICODE and UNICODE2ANSI

works file in MT5. tested with bad password and the translatd message comes out fine.

I have exactly the same code  in MT4/MQL 4.

Gives my errno  2003  and untranslatable errormessage  

?

 
I can assume that MT4 accepts ANSI strings while MT5 works with Unicode.
 

I have seen a message that Build 600 of MT4 and higher does not support ANSI.

I started out with ANSI on MT4.

 
ingvar_e:

I have seen a message that Build 600 of MT4 and higher does not support ANSI.

I started out with ANSI on MT4.

Yes MT4 uses now Unicode string.

How can we help ? Can you provide a snippet of code to demonstrate your issue ?

 
angevoyageur:

Yes MT4 uses now Unicode string.

How can we help ? Can you provide a snippet of code to demonstrate your issue ?

 

ingvar_e

Code in MQL4 EA and MQL5 EA

#ifdef __MQL5__
#import "MySQLFunc.ex5"
 bool SetParms(string host,string user, string pass,string signalsDB,string signalsTable,string parmsTable,uint port);
 bool connectToSQL();
 bool CreateParmsTable();
#import
#else
#import "MySQLFunc.ex4"
 bool SetParms(string host,string user, string pass,string signalsDB,string signalsTable,string parmsTable,uint port);
 bool connectToSQL();
 bool CreateParmsTable();
#import
#endif




Code in MQL4  and MQL5.  Resides in one ex4 library and one ex5 library:

bool connectToSQL() export
 {
  string moduleID = "[ConnectToSQL] ";

  
     MyDB2   = UNICODE2ANSI(MyDB);
     MyHost2 = UNICODE2ANSI(MyHost);
     MyUser2 = UNICODE2ANSI(MyUser);
     MyPass2 = UNICODE2ANSI(MyPass); 
/*
     MyDB2   = MyDB;               //if before build 600
     MyHost2 = MyHost;
     MyUser2 = MyUser;
     MyPass2 = MyPass;
 */

  
  mysql = mysql_init(mysql);                //This works ok in MT4 and MT5
  if(mysql == 0)
   {
    Print(moduleID+"Init mysql_init failed!");
    mysqlOK = false;
    return(false);
   }
   
   Print("Init mysql_init ok:  " + mysql);
   
  int res =  mysql_real_connect(mysql,MyHost2,MyUser2,MyPass2,MyDB2,MyPort,"",0);
    
    if(res != mysql)
     {
       Print(moduleID+" Connectfailed res :  " + res + "  mysql: " + mysql);
       Print("errno: " + mysql_errno(mysql));
       string ww = mysql_error(mysql);
       Print("error: " + ww);
       string ww2 = ANSI2UNICODE(ww);
       Print("Error translated : " + ww2);
       
       return false; 
     }

     
   return true;
 }
 

Results:

running MT5 with correct PW

Mysql_init OK

Print:    "Tables created"

Running in MT5 with bad PW

mysql_init ok

Print:  "Error 1045"

Print   A lot of japanese symbols

Print translated: Access denied for user

So MT5 behaves as expected

Running MT4 with correct PW

Print "error:  2003

Print Just a bunch of question marks and a couple of number 4 digits

Print translated:  nothing, blank




 
ingvar_e:

It probably means there is something in UNICODE2ANSI that doesn't work with mql4.

Why not simply use StringToCharArray() ?

StringToCharArray - MQL4 Documentation
  • docs.mql4.com
StringToCharArray - MQL4 Documentation
 
angevoyageur:

It probably means there is something in UNICODE2ANSI that doesn't work with mql4.

Why not simply use StringToCharArray()?


Interesting thought. Will investigate. Thanks

Hmm. Nobody been using MT4 >600 with MySQL?

 

Been looking around. Problem has been around for quite  a while. No good solutions found.

What would be nice is MQL4 source for ANSI2UNICODE and UNICODE2ANSI. I really do not like to use "wrappers" which seems to be the solution suggested

I am using a Library for my MySQL functions and I would like it o be common for MQL5 and MQL4 (Compiler directives accepted) and it has these functions in MQL5

 
ingvar_e:
Can I suggest you to not answer inside the quote. It's very annoying. Thank you.
 

Sorry about that

Found the solution. The originator of the ANSI2UNICODE and UNICODE2ANSI had made an update to the original code from 2010

http://mqlmagazine.com/mql-programming/dll-hell-mql5-edition-unicode-vs-ansi/

Look in comments!

Very simple to change with compiler directives so that the code works both in MQL4 and MQL5

Tested OK, Case closed

DLL Hell, MQL5 edition : UNICODE vs ANSI | MQLmagazine.com
  • mqlmagazine.com
Many many years ago, when we were kids, in the beginning years of the crazy 90s, two languages were in battle in developer world. Pascal, with a down-to-earth, easy to understand syntax, well suited to a high level language, and C++, with a more cryptic, but faster to use syntax, well suited to its medium level. C++ won the battle, and...
Reason: