#1 2014-11-29 20:49:17

tedroper
Member
Registered: 2011-12-24
Posts: 4

Updating datetime in Sample Project02

This is the AddButtonClick procedure from you sample projects.
i want the sample to search for a record that may match the name entered and then update the record instead of adding the record at the end of the database.

1. i added sRec: TSQLSampleRecord to search the database for the old record.
2. if found, i want to update the datetime and the question.
3. The datetime is TModTime type, so i'm using sRec.Time := TimeLogNow;  (which puts my computer time in db).
4. if Database.Add(Rec,true, false, true)=0 then  //updates time correctly, the last true updates to my time i think.
5. if not Database.Update(sRec) then  //DOES NOT UPDATE DATETIME CORRECTLY
note:  i have tried Database.Update(sRec, [0..2]) and other updates, but can not get the update to change with my time.

Please advise how to code the update to update the question and time?
tks,
ted


//SAMPLE CODE FROM PROJECT
procedure TForm1.AddButtonClick(Sender: TObject);
var Rec, sRec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create;
  try
    sRec := TSQLSampleRecord.Create(Database,'Name=?',[StringToUTF8(NameEdit.Text)]);
    sRec.Name := StringToUTF8(NameEdit.Text);
    sRec.Question := StringToUTF8(QuestionMemo.Text);
    sRec.Time := TimeLogNow;
    // we use explicit StringToUTF8() for conversion below
    // a real application should use TLanguageFile.StringToUTF8() in mORMoti18n
    Rec.Name := StringToUTF8(NameEdit.Text);
    Rec.Question := StringToUTF8(QuestionMemo.Text);
    Rec.Time := TimeLogNow;
    if sRec.ID>0 then begin
      if not Database.Update(sRec) then  //DOES NOT UPDATE DATETIME CORRECTLY
      ShowMessage('Error updating the data') else begin
       NameEdit.Text := '';
       QuestionMemo.Text := '';
       NameEdit.SetFocus;
      end;
    end else
    if Database.Add(Rec,true, false, true)=0 then  //updates time correctly
      ShowMessage('Error adding the data') else begin
       NameEdit.Text := '';
       QuestionMemo.Text := '';
       NameEdit.SetFocus;
    end;
  finally
    sRec.Free;
    Rec.Free;
  end;
end;

Offline

#2 2014-11-29 20:55:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,658
Website

Re: Updating datetime in Sample Project02

TModTime fields are set by the CRUD methods.
Any manually set value will be overridden.
Use TTimeLog or TDateTime for a value set by your code.

Online

#3 2014-11-30 19:05:24

tedroper
Member
Registered: 2011-12-24
Posts: 4

Re: Updating datetime in Sample Project02

tks for the reply.
TTimeLog does update when used.

i have to handle datetime with daylight savings time.  is TModTime a better way to handle daylight savings time.  the values saved is 8 hours ahead of my local time.  i am not sure what is meant by ServerTime?

tks for your help
ted

Offline

#4 2014-11-30 19:32:50

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,658
Website

Re: Updating datetime in Sample Project02

All types are expected to be UTC. They do not contain any time zone.
It is to the application layer to display the dates in local time.

Online

Board footer

Powered by FluxBB