Recent

Author Topic: DBGrid refresh when new records appear in the table  (Read 1813 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
DBGrid refresh when new records appear in the table
« on: September 14, 2020, 08:58:52 pm »
I'm using TDBGrid to output data from Informix DB via ODBC driver.
Dataset Close / Open is used to reread the data from the DB on user's request.
Now I have to make DBGrid to refresh automatically.
Of course, I can initiate Dataset Close / Open every minute with Timer.
But maybe there is a more clever solution available?
E.g., update the DBGrid only when new record is added to the underlying table.
And not to load the whole table, but only the new records.
(Old records are never changed. Old records are deleted after a year, while I have to output data for current day only)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: DBGrid refresh when new records appear in the table
« Reply #1 on: September 15, 2020, 09:55:04 am »
How do you know there are new records?
If you know that you can do a refresh of your DBGrid
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: DBGrid refresh when new records appear in the table
« Reply #2 on: September 15, 2020, 11:07:03 am »
How do you know there are new records?
This is what I'm asking for.
I'm not an expert in DB engines, I just supposed that DB engine may provide some event when the monitored table is changed (not sure this will work via ODBC).
In a worse case, application can request number of records in a table and re-read the whole table (or better only delta) when it changes.
I hoped this is a common task when working with DBs and it have a well-known solution.

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: DBGrid refresh when new records appear in the table
« Reply #3 on: September 15, 2020, 11:24:13 am »
I take it your DBGrid has a Tdatasource on which it is based.
The Tdatasource has an event called OnDataChange, that's the place you can code your action (DBGrid.refresh or something like that).
In the Object Properties of the datasource click the tab titled 'Events' and doubleclick in the column behind 'OnDataChange'.
Hope this helps you.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: DBGrid refresh when new records appear in the table
« Reply #4 on: September 15, 2020, 12:13:02 pm »
The Tdatasource has an event called OnDataChange, that's the place you can code your action (DBGrid.refresh or something like that).
In the Object Properties of the datasource click the tab titled 'Events' and doubleclick in the column behind 'OnDataChange'.
TDatasource.OnDataChange has nothing to do with changing data in the database.
It is called when your program changes a field-value.
https://www.freepascal.org/docs-html/current/fcl/db/tdatasource.ondatachange.html

TS wants to have a trigger for when data on the server-side changes.
And that's very specific to what database is used.
i.e. Firebird has monitor-events for that. Not sure about Informix DB but via an ODBC drive it would really pose a challenge (i.e. not even sure it's possible).

And even if it was possible, you would also need to examine what type of transaction is used. Without closing and opening a new transaction, new and changed records might not even be available to the current transaction.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: DBGrid refresh when new records appear in the table
« Reply #5 on: September 15, 2020, 12:38:21 pm »
one possible soloution is, to use a trigger and write the actual date in a simple flagging table with one row. The query to that simple table will normally held in memory and an access to this table/field willbe without stressing the server. if a new row araise the trigger will change the field. Polling will be the only way to get information. Callback are normally not implemented with serverbased DBs
regards
Andreas

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: DBGrid refresh when new records appear in the table
« Reply #6 on: September 15, 2020, 12:41:32 pm »
Callback are normally not implemented with serverbased DBs
Actually the are.
See https://firebirdsql.org/file/documentation/papers_presentations/Power_Firebird_events.pdf
And I'm sure Oracle, Microsoft SQL Server etc have them too.

But the problem is implementing them.
And especially through ODBC which TS is using.

 

TinyPortal © 2005-2018