Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FireDac query not reading large integers correctly

Tags:

delphi

I am trying to query a database using FireDac. Here is my code;

procedure TfSMSViewer.LoadSMSFromDatabase(path: AnsiString);
var
  con: TFDConnection;
  query: TFDQuery;
  LI: TListItem;
  B: Int64;
begin
 con := TFDConnection.Create(nil);
  query := TFDQuery.Create(con);
  con.LoginPrompt := False;
  con.Open('DriverID=SQLite;Database=' + Path +' ;');
  query.Connection := con;
  query.SQL.Text := 'SELECT * FROM sms';
  query.Open;
  query.First;
  While Not Query.EOF Do
  Begin
    LI := ListView1.Items.Add;
    LI.Caption := inttostr(query.Fields[4].AsLargeInt); //This line
    if query.FieldValues['type'] = 1 then
      LI.SubItems.Add('Incoming')
    else
      LI.SubItems.Add('Outbound');

    LI.SubItems.Add(query.FieldValues['address']);
    LI.SubItems.Add(query.FieldValues['body']);
    Query.Next;
  End;
end;

However the line highlighted doesn't work correctly. In the database, an example value set in this column is 1418421520957 (a UNIX timestamp).

When that line of code is executed, the result is 1082313277.

The data type in the SQLite database is set to Integer. The freeware software I'm using to debug this shows the correct value. When debugging my code, the incorrect value is pulled from the database before any assignment is made.

Also some of the values populated in my TListView are negated. Values loaded into the listview from my code

Does TFDQuery not support large integers? How can I fix this?

Thanks

like image 285
Jake Evans Avatar asked Sep 11 '25 21:09

Jake Evans


1 Answers

This is how I fixed it, with suggestions from TLama. Maybe this will be useful for someone, or my future self.

  con := TFDConnection.Create(nil);
  query := TFDQuery.Create(con);
  with query.FormatOptions do begin
    OwnMapRules := True;
    with MapRules.Add do begin
      SourceDataType := dtInt32;
      TargetDataType := dtInt64;
    end;
  end;
like image 185
Jake Evans Avatar answered Sep 15 '25 13:09

Jake Evans