Recent

Author Topic: [solved] How to edit and save DBGrid record?  (Read 5401 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
[solved] How to edit and save DBGrid record?
« on: January 20, 2014, 11:30:35 pm »
I have a SQLite table with countries in. It loads fine into a DBGrid with TQuery and TDataSource. But when I try to insert or edit a record, nothing can be typed in the cells and nothing is saved to database.

Used Query.InsertSQL, but I still cannot type anything in the text cells:

INSERT INTO countries(COUNTRY_ID, COUNTRY_NAME) VALUES(:COUNTRY_ID,:COUNTRY_NAME);

Some working sample code would be appreciated. :)

Attaching a small test app with a SQLite database.db3. Remember to change the database path in the dbconnection if you test it.

Thanks in advance!
« Last Edit: January 24, 2014, 05:10:12 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: How to edit and save DBGrid record?
« Reply #1 on: January 21, 2014, 12:11:50 am »
You have a few issues:

The immediate problem being that you created your tables with TEXT columns which SQLDB treats as TMemofields (not editable in grid as they stand). Use VARCHAR instead and SQLDB will return TStringfields.

Also: I do not see you doing any ApplyUpdates; search the forum - there must be dozens of posts related to that.

Finally:

Code: [Select]
  //Simply prevents the hint about parameter Astate not used.
  if gdSelected in AState then;                             

The proper way to do this is to put a '{%H-}' in front of the parameter for which you want to suppress a hint (see: http://wiki.freepascal.org/IDE_directives )
« Last Edit: January 21, 2014, 12:18:45 am by kpeters58 »
Lazarus 2.0.4/FPC 3.0.4/Win 64

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: How to edit and save DBGrid record?
« Reply #2 on: January 22, 2014, 12:01:04 am »
Thanks for the help!

I have changed from Text to VARCHAR(50) in SQLite database and it doesn't say just (memo) in the cells anymore. :)

Then in the AfterPost event of the Query I added:

Query.ApplyUpdates
Transaction.CommitRetaining

(Commit closes the dataset, but commitretaining keeps it active)
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: How to edit and save DBGrid record?
« Reply #3 on: January 22, 2014, 05:53:21 am »
That should do it. 

BTW, there is no point (though it also doesn't hurt) to specify VARCHAR(<length) as SQLite ignores it anyway.
A simple VARCHAR suffices so you can save the typing (unless you want this for yourself for documentation purposes or such).

Lazarus 2.0.4/FPC 3.0.4/Win 64

 

TinyPortal © 2005-2018