Lavorare con i database

Le funzioni per lavorare con i database usano il popolare e facile-da-usare motore SQLite. Una caratteristica importante di questo motore è che l'intero database è inserito in un singolo file standard situato sul PC dell'utente.

The functions allow for convenient creation of tables, adding data to them, performing modifications and sampling using simple SQL requests:

  • receiving trading history and quotes from any formats,
  • saving optimization and test results,
  • preparing and exchanging data with other analysis packages,
  • storing MQL5 application settings and status.

Queries allow using statistical and mathematical functions.

The functions for working with databases allow you to replace the most repetitive large data array handling operations with SQL requests, so that it is often possible to use the DatabaseExecute/DatabasePrepare calls instead of programming complex loops and comparisons. Use the DatabaseReadBind function to conveniently obtain query results in a ready-made structure. The function allows reading all record fields at once within a single call.

To accelerate reading, writing and modification, a database can be opened/created in RAM with the DATABASE_OPEN_MEMORY flag, although such a database is available only to a specific application and is not shared. When working with databases located on the hard disk, bulk data inserts/changes should be wrapped in transactions using DatabaseTransactionBegin/DatabaseTransactionCommit/DatabaseTransactionRollback. This accelerates the process hundreds of times.

Per iniziare a lavorare con le funzioni, leggi l'articolo SQLite: gestione nativa dei database SQL in MQL5.

Funzione

Azione

DatabaseOpen

Apre o crea un database in un file specificato

DatabaseClose

Chiude un database

DatabaseImport

Importa i dati da un file in una tabella

DatabaseExport

Esporta una tabella o un risultato di esecuzione di una richiesta SQL in un file CSV

DatabasePrint

Stampa una tabella o un risultato di esecuzione di una richiesta SQL nel journal degli experts

DatabaseTableExists

Verifica la presenza della tabella in un database

DatabaseExecute

Esegue una richiesta ad un database specificato

DatabasePrepare

Crea un handle di una richiesta, che può quindi essere eseguita utilizzando DatabaseRead()

DatabaseReset

Reimposta una richiesta, come dopo aver chiamato DatabasePrepare()

DatabaseBind

Imposta un valore di parametro in una richiesta

DatabaseBindArray

Imposta un array come valore di parametro

DatabaseRead

Passa alla voce successiva a seguito di una richiesta

DatabaseFinalize

Rimuove una richiesta creata in DatabasePrepare()

DatabaseTransactionBegin

Inizia l'esecuzione della transazione

DatabaseTransactionCommit

Completa l'esecuzione della transazione

DatabaseTransactionRollback

Ripristina le transazioni

DatabaseColumnsCount

Ottiene il numero di campi in una richiesta

DatabaseColumnName

Ottiene un nome campo per indice

DatabaseColumnType

Ottiene un tipo di campo per indice

DatabaseColumnSize

Ottiene una dimensione del campo in byte

DatabaseColumnText

Ottiene un valore di campo come stringa dal record corrente

DatabaseColumnInteger

Ottiene il valore del tipo int dal record corrente

DatabaseColumnLong

Ottiene il valore di tipo long dal record corrente

DatabaseColumnDouble

Ottiene il valore di tipo double dal record corrente

DatabaseColumnBlob

Ottiene un valore di campo come un array dal record corrente

Queries allow using statistical and mathematical functions.

 

Statistical functions:

  • mode – mode
  • median – median (50th percentile)
  • percentile_25 – 25th percentile
  • percentile_75
  • percentile_90
  • percentile_95
  • percentile_99
  • stddev or stddev_samp — sample standard deviation
  • stddev_pop — population standard deviation
  • variance or var_samp — sample variance
  • var_pop — population variance

Mathematical functions

Example:

select
  count(*) as book_count,
  cast(avg(parentas integeras mean,
  cast(median(parentas integeras median,
  mode(parentas mode,
  percentile_90(parentas p90,
  percentile_95(parentas p95,
  percentile_99(parentas p99
from moz_bookmarks;