Forum > Databases
I need basic help with DB component interaction
yes:
Hej,
I need some basic help with the interaction of the visual DB components, maybe a hint for what to reread.
Situation:
I want to display data in a DBGrid. When selected I want to display dependent information in a second DBGrid. It is 1:n, e.g. a user and a list of associated notes (by a foreign key in the notes table). I use SQLite. The basic part works due to the nice tutorials in the wiki.
Idea:
SQLiteConnection, SQLTransaction, SqlQuery1, DataSource1 for DBGrid1. Event OnDataChange in DataSource1 fires; then I execute SqlQuery2 for DataSource2 for DBGrid2 for the dependent data. Of course no second connection or transaction is used.
Problem:
The event is called but the dependent data is loaded only once (at first call). The data for the first call is displayed correctly in the DBGrid2 but no changes are shown for subsequent calls.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);begin SqlNotiz.Sql.Text:='select * from notes n where n.id_user = :user'; SqlNotiz.Params.ParamByName('user').AsInteger:=DataSource1.DataSet.Fields[0].AsInteger; SqlNotiz.Open; Label1.Caption:='Changed to '+DataSource1.DataSet.Fields[0].AsString;end;
It's obvious that I miss something basic like refreshing the sql statement or properly closing it. I would be thankful for any hints (also about hints how to do it differently if the whole approach is done in other ways normally or better).
GAN:
Assuming SqlNotiz is your datset2, you may try this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);var iUser:Interger;begin iUser:=DataSource1.DataSet.Fields[0].AsInteger; SqlNotiz.Close; SqlNotiz.Sql.Text:='select * from notes n where n.id_user = :iuser'; SqlNotiz.Params.ParamByName('user').AsInteger:=iUser; SqlNotiz.Open; Label1.Caption:='Changed to '+IntToStr(iUser);end;
And yes, you need to close first, that's why the first time it works fine and then it doesn't.
cdbc:
Hi
Did a quick search on /gockle/ "master-detail in sqlite and freepascal" and these 2 are the first hits:
https://wiki.freepascal.org/TSqlite3_Master_Detail_Example
https://wiki.freepascal.org/MasterDetail
If you're more into videos, here's one about Delphi, but that's practically the same: https://www.youtube.com/watch?v=UmZYVB8n8bc
HTH
Regards Benny
yes:
Thank you for your help.
Yes, I forgot to change the localized name for the post in that case. It works now.
Of course I could have sworn that I tried closing it...
yes:
--- Quote from: cdbc on July 26, 2024, 10:55:04 pm ---Hi
Did a quick search on /gockle/ "master-detail in sqlite and freepascal" and these 2 are the first hits:
https://wiki.freepascal.org/TSqlite3_Master_Detail_Example
https://wiki.freepascal.org/MasterDetail
--- End quote ---
Thank you, that looks like the intended way to go, and helped me to grasp a bit more of the ideas behind the components.
Navigation
[0] Message Index
[#] Next page