DatabaseExport

Exports a table or an SQL request execution result to a CSV file. The file is created in the UTF-8 encoding.

long  DatabaseExport(
   int           database,           // database handle received in DatabaseOpen
   const string  table_or_sql,       // a table name or an SQL request
   const string  filename,           // a name of a CSV file for data export
   uint          flags,              // combination of flags
   const string  separator           // data separator in the CSV file
   );

Parameters

database

[in]  Database handle received in DatabaseOpen().

table_or_sql

[in]   A name of a table or a text of an SQL request whose results are to be exported to a specified file.

filename

[in]  A file name for data export. The path is set relative to the MQL5\Files folder.

flags

[in]  Combination of flags defining the output to a file. The flags are defined as follows:

DATABASE_EXPORT_HEADER                 – display a string with field names
DATABASE_EXPORT_INDEX                    – display string indices
DATABASE_EXPORT_NO_BOM                – do not insert BOM mark at the beginning of the file (BOM is inserted by default)
DATABASE_EXPORT_CRLF                     – use CRLF for line break (the default is LF)
DATABASE_EXPORT_APPEND                 – add data to the end of an existing file (by default, the file is overwritten). If the file does not exist, it will be created.
DATABASE_EXPORT_QUOTED_STRINGS – display string values in double quotes.
DATABASE_EXPORT_COMMON_FOLDER - a CSV file is created in the common folder of all client terminals \Terminal\Common\File.

separator

[in]  Data separator. If NULL is specified, the '\t' tabulation character is used as a separator.  An empty string "" is considered a valid separator but the obtained CSV file cannot be read as a table – it is considered as a set of strings.

 

Return Value

Return the number of exported entries or a negative value in case of an error. To get the error code, use GetLastError(), the possible responses are:

  • ERR_INTERNAL_ERROR (4001)                       – critical runtime error;
  • ERR_INVALID_PARAMETER (4003)                  – path to the database file contains an empty string, or an incompatible combination of flags is set;
  • ERR_NOT_ENOUGH_MEMORY (4004)              - insufficient memory;
  • ERR_FUNCTION_NOT_ALLOWED(4014)           – specified pipe is not allowed;
  • ERR_PROGRAM_STOPPED(4022)                    – operation canceled (MQL program stopped);
  • ERR_WRONG_FILENAME (5002)                     - invalid file name;
  • ERR_TOO_LONG_FILENAME (5003)                 - absolute path to the file exceeds the maximum length;
  • ERR_CANNOT_OPEN_FILE(5004)                    – unable to open the file for writing;
  • ERR_FILE_WRITEERROR(5026)                      – unable to write to the file;
  • ERR_DATABASE_INTERNAL (5120)                 – internal database error;
  • ERR_DATABASE_INVALID_HANDLE (5121)      - invalid database handle;
  • ERR_DATABASE_QUERY_PREPARE(5125)        – request generation error;
  • ERR_DATABASE_QUERY_NOT_READONLY       – read-only request is allowed.

 

Note

If request results are exported, the SQL request should begin with "SELECT" or "select". In other words, the SQL request cannot alter the database status, otherwise DatabaseExport() fails with an error.

Database string values may contain the conversion character ('\r' or '\r\n' ), as well as the value separator character set in the separator parameter. In this case, be sure to use the DATABASE_EXPORT_QUOTED_STRINGS flag in the 'flags' parameter. If this flag is present, all displayed strings are enclosed in double quotes. If a string contains a double quote, it is replaced by two double quotes.

 

See also

DatabasePrint, DatabaseImport