Recent

Author Topic: [SOLVED] update sqlite record in dbgrid  (Read 7408 times)

koloth

  • New Member
  • *
  • Posts: 14
    • http://users.forthnet.gr/chi/pitbull/
[SOLVED] update sqlite record in dbgrid
« on: May 18, 2015, 10:51:45 am »
Helloi all,
I'm building a small db app using sqlite to familiarize myself with lazarus db components, being a delphi developer for some years before.
I've ran into to the following issue:
In order to replicate the ttable control, i'm using a tquery containing a select * of the table in question.
However although  the dbgrid displays the data correctly, every change i make is not submitted to the DB after closing the app.
autoedit is enabled on  the dataset, i've tried committing the transaction and applyupdates from the query.
Can someone point me to the proper way of updating a table using the dbgrid?

thank you
« Last Edit: May 25, 2015, 11:17:12 am by koloth »

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: update sqlite record in dbgrid
« Reply #1 on: May 18, 2015, 08:18:51 pm »
Try read for example: http://wiki.freepascal.org/SqlDBHowto and related tutorials ...

koloth

  • New Member
  • *
  • Posts: 14
    • http://users.forthnet.gr/chi/pitbull/
Re: update sqlite record in dbgrid
« Reply #2 on: May 19, 2015, 09:49:45 am »
Try read for example: http://wiki.freepascal.org/SqlDBHowto and related tutorials ...

Thanks for the reply, I had already read that but i'm glad i was on the right track...
If my understanding is correct i have to create a custom update script for the query, is there a guideline for that too?

tx a lot

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: update sqlite record in dbgrid
« Reply #3 on: May 19, 2015, 11:27:12 am »
No, that's not true. Only if you using joins in your table it's necessary using updateSQL.
The most important thing is the following procedures:
- post
- applyupdate
- commit(retaining)
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

koloth

  • New Member
  • *
  • Posts: 14
    • http://users.forthnet.gr/chi/pitbull/
Re: update sqlite record in dbgrid
« Reply #4 on: May 20, 2015, 11:36:36 am »
No, that's not true. Only if you using joins in your table it's necessary using updateSQL.
The most important thing is the following procedures:
- post
- applyupdate
- commit(retaining)

On which dbgrid event should the procedure be applied to?

thank you..

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: update sqlite record in dbgrid
« Reply #5 on: May 20, 2015, 01:45:48 pm »

On which dbgrid event should the procedure be applied to?

I would use TDataSet.AfterPost event. Not any TDBGrid event!

koloth

  • New Member
  • *
  • Posts: 14
    • http://users.forthnet.gr/chi/pitbull/
Re: update sqlite record in dbgrid
« Reply #6 on: May 20, 2015, 02:50:28 pm »
I would use TDataSet.AfterPost event. Not any TDBGrid event!
[/quote]
In't it a bit weird to run :
Code: [Select]
- post
- applyupdate
- commit(retaining)

in the afterpost event? will it ever run?

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: update sqlite record in dbgrid
« Reply #7 on: May 20, 2015, 09:18:01 pm »
Of course not "Post" in "AfterPost" event handler.

DB-aware controls like DBGrid or DBNavigator are calling Post.
Which is okay.

But I would use remaining 2 calls "ApplyUpdates" and "Commit" in AfterPost (best in DataModule)

So you will have isolated "presentation of data" (forms) and "background data manipulation/interaction with database")

koloth

  • New Member
  • *
  • Posts: 14
    • http://users.forthnet.gr/chi/pitbull/
Re: update sqlite record in dbgrid
« Reply #8 on: May 25, 2015, 11:13:47 am »
thanks for your help guys, finally nailed it!

moving on to updating joined queries... :)

 

TinyPortal © 2005-2018