Question

The application is datasnap (with sqlite database). I run the following query against two tables (LOKACIJE and UPORABNIKI):

procedure TForm4.FormShow(Sender: TObject);
begin
   ClientDataSet1.Close;
   ClientDataSet1.CommandText :='';
   ClientDataSet1.CommandText :=  'select  lokacije.[HOTEL_ID],'+
                                  '        lokacije.[NAZIV],'+
                                  '        uporabniki.[LOKACIJA_ID],'+
                                  '        uporabniki.[RESORT_ID],'+
                                  '        uporabniki.[HOTEL_ID],'+
                                  '        uporabniki.[UPORABNIK],'+
                                  '        uporabniki.[GESLO],'+
                                  '        uporabniki.[PRAVICE]'+
                                  '        from UPORABNIKI'+
                                  '        inner join LOKACIJE on uporabniki.lokacija_id=lokacije.lokacija_id '+
                                  ' where lokacije.[NAZIV] = :@NAZIV'+
                                  ' order by Uporabniki.[UPORABNIK]';
 ClientDataSet1.Params.ParamByName('@NAZIV').Value:= '' + Form2.AdvOfficeStatusBar1.Panels[3].Text + '' ;
 ClientDataSet1.Open;
end;

The query runs fine and gives me the desired results. However I would like to be able to edit and save the edited (or added) results of this query. The table that I want to update (or add the new record) is the UPORABNIKI. I do not need to write anything to the LOKACIJE table. How am I to accomplish this ?

Apart from saving new record I would like the query to fill automatically the values LOKACIJA_ID,RESORT_ID,HOTEL_ID as they are from the same table, when I click in the navigator the button 'insert'. UPORABNIKI is translated USERS table.

edit : Inverted the query as suggested

Was it helpful?

Solution

I believe this is a case in which TDatasetProvider is unable to produce the right command to update the tables involved. In this cases, what I do is to add a handler to the TDatasetProvider.BeforeUpdateRecord event. This event will let you handle each operation worked on the dataset and produce the needed SQL statements to propertly persist those operations in the data server.

You will have to write the UPDATE/DELETE/INSERT statements yourself, but you will also have absolute power about how the tables are updated. That´s why I always use this event and never rely on TDatasetProvider intrinsec update process.

OTHER TIPS

See the OnGetTableName event on the TDatasetProvider.

Also, I believe it would be better if you invert your query, i.e., use ... FROM UPORABNKI inner join LOKACIJE...

The OnGetTableName event from the TDataSetProvider has a var parameter called TableName. You should assign it to 'UPORABNIKI'.

Also, usually TDataSetProvider thinks that the table you use on the FROM clause is the one you want to update, so, if you change your query, the above event might not even be needed.

Hope that helps, Marco

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top