- //+------------------------------------------------------------------+
- //| Script program start function |
- //+------------------------------------------------------------------+
- void OnStart()
- {
- string Host, User, Password, Database, Socket; // database credentials
- int Port, ClientFlag;
- int DB; // database identifier
- Print (MySqlVersion());
- string terminal_data_path = TerminalInfoString(TERMINAL_DATA_PATH);
- string strDbConfig = terminal_data_path + "\\MQL4\\Scripts\\MyConnection.ini";
- // reading database credentials from INI file
- Host = ReadIni(strDbConfig, "MYSQL", "Host");
- User = ReadIni(strDbConfig, "MYSQL", "User");
- Password = ReadIni(strDbConfig, "MYSQL", "Password");
- Database = ReadIni(strDbConfig, "MYSQL", "Database");
- Port = StrToInteger(ReadIni(strDbConfig, "MYSQL", "Port"));
- Socket = ReadIni(INI, "MYSQL", "Socket");
- ClientFlag = StrToInteger(ReadIni(strDbConfig, "MYSQL", "ClientFlag"));
- Print ("Host: ", Host, ", User: ", User, ", Database: ", Database);
- // open database connection
- Print ("Connecting...");
- DB = MySqlConnect(Host, User, Password, Database, Port, Socket, ClientFlag);
- if (DB == -1)
- {
- Print ("Connection failed! Error: " + MySqlErrorDescription);
- }
- else
- {
- Print ("Connected! DBID#", DB);
- }
- string Query;
- Query = "DROP TABLE IF EXISTS `test_table`";
- MySqlExecute(DB, Query);
- Query = "CREATE TABLE `test_table` (id int, code varchar(50), start_date datetime)";
- if (MySqlExecute(DB, Query))
- {
- Print ("Table `test_table` created.");
- }
- else
- {
- Print ("Table `test_table` cannot be created. Error: ", MySqlErrorDescription);
- }
- MySqlDisconnect(DB);
- Print ("Disconnected. Script done!");
- }
复制代码
|