- Principles of database operations in MQL5
- SQL Basics
- Structure of tables: data types and restrictions
- OOP (MQL5) and SQL integration: ORM concept
- Creating, opening, and closing databases
- Executing queries without MQL5 data binding
- Checking if a table exists in the database
- Preparing bound queries: DatabasePrepare
- Deleting and resetting prepared queries
- Binding data to query parameters: DatabaseBind/Array
- Executing prepared queries: DatabaseRead/Bind
- Reading fields separately: DatabaseColumn Functions
- Examples of CRUD operations in SQLite via ORM objects
- Transactions
- Import and export of database tables
- Printing tables and SQL queries to logs
- Example of searching for a trading strategy using SQLite
Checking if a table exists in the database
The built-in DatabaseTableExists function allows you to check the existence of a table by its name.
bool DatabaseTableExists(int database, const string table)
The database descriptor and the table name are specified in the parameters. The result of the function call is true if the table exists.
Let's extend the DBSQLite class by adding the hasTable method.
class DBSQLite
|
The script DBcreateTable.mq5 will check if the table has appeared.
void OnStart()
|
Again, don't worry about potentially getting an error when trying to recreate. This does not affect the existence of the table in any way.
database error, table table1 already exists
|
Since we are writing a generic helper class DBSQLite, we will provide a mechanism for deleting tables in it. SQL has the DROP command for this purpose.
class DBSQLite
|
Before executing the query, we check for the existence of the table and immediately exit if it does not exist.
After executing the query, we additionally check whether the table has been deleted by calling DatabaseTableExists again. Since the absence of a table will be flagged with the DATABASE_NO_MORE_DATA error code, which is the expected result for this method, we clear the error code with ResetLastErrorOnCondition.
It can be more efficient to use the capabilities of SQL to exclude an attempt to delete a non-existent table: just add the phrase "IF EXISTS" to the query. Therefore, the final version of the method deleteTable is simplified:
bool deleteTable(const string name) const
|
You can try to write a test script for deleting the table, but be careful not to delete a working table by mistake. Tables are deleted immediately with all data, without confirmation and without the possibility of recovery. For important projects, keep database backups.