The database connection operation can be time consuming. Therefore often connecting/disconnecting a database may cause application overload. That is why it is recommended to connect the database after the application is launched and disconnect the database when the application is stopping.
It is recommended to close the database after all requested database operations are completed (usually after the application is stopped).
While working with databases it is necessary to verify whether the operation was completed successfully or failed.
for database operations in the PROMOTIC system can be divided into two basic approaches:
Reading the data into the application
The order of operations while reading the data into the application by the
PmaAdo object:
- connect to the database
- open the corresponding table
- process data in the application
- close current table
- (optional: close the database)
The mentioned operations are demonstrated in the following example:
Writing data from the application to the database
There are two possible ways of writing the data:
1. Writing data into the table without previously opening it
2. Open the table and add a new record into this open table
On writing/changing the data the first way, the following steps must be completed:
- connect to the database
- write/change data in the database
- (optional: close database)
This data writing approach uses SQL statements
INSERT or
UPDATE.
On writing/changing the data the second way, the following steps must be completed:
- Connect to the database
- Open table and eventually find the corresponding record
- write/change data in the database
- (optional: close database)
This data writing approach uses methods of objects
PmaAdo,
AdoRecordset etc.
The first approach is more considerate to the application and the database because it is not necessary to open each table and load the corresponding rows into the memory. New record can be added just by using the
INSERT INTO statement (see
The usage of the statement insert into). Current record can be changed by the
UPDATE statement (see
The usage of the statement update). These SQL statements must be written
in the SQL language syntax of the corresponding database system. This approach therefore requires knowledge of the SQL language of the corresponding database that is being used.
The second approach does not require detailed knowledge of the SQL language but it is more demanding (especially from the memory point of view) on the application and the database, because adding a new record requires opening (=loading) the table (see
The usage of the "AddNew" method). When changing the data it is necessary to find the corresponding record that can then be modified (see
Edit the current record). Therefore this approach is recommended to be used only with tables that contain only a small number of records. It is also possible to modify the SQL query for table opening so it returns only a selected portion of records (ideally only a single record) - e.g. by using complex SQL queries e.g. as this:
"select * from table1 where ctime = (select max(ctime) from table1)"
For this purpose it is not recommended using the limiting clauses like
select TOP,
LIMIT etc.