# 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()](/en/docs/database/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 from the [ENUM_DATABASE_EXPORT_FLAGS](/en/docs/database/databaseexport) enumeration. 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()](/en/docs/check/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. ENUM_DATABASE_EXPORT_FLAGS | ID | Description | | --- | --- | | DATABASE_EXPORT_HEADER | Display field names in the first string | | 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 string 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. | Example: ``` input int InpRates=100; //+------------------------------------------------------------------+ //| Script program start function                                    | //+------------------------------------------------------------------+ void OnStart()  {   MqlRates rates[]; //--- remember the start time before receiving bars   ulong start=GetMicrosecondCount(); //--- request the last 100 bars on H1   if(CopyRates(Symbol(), PERIOD_H1, 1, InpRates, rates)0)     Print("Table RATES saved in ", Symbol(), ".csv");   else     Print("DatabaseExport() failed. Error ", GetLastError()); //--- close the database file and inform of that   DatabaseClose(db);   PrintFormat("Database: %s created and closed", filename); ``` See also [DatabasePrint](/en/docs/database/databaseprint), [DatabaseImport](/en/docs/database/databaseimport)