dimanche 8 mai 2016

Data loss with SQLite database in Delphi

I am trying to save binary encoded data in SQLite database and I am able to save the values but there are few characters that are getting lost after saving and closing the dataset.

The inserted data looks like this.

enter image description here

The highlighted text is getting lost when I load the saved record in a grid or table.

Create SQLite connection:

procedure CreateSQLiteDB(ASQLiteDB: string);
begin
  FDConnection1.Params.Values['Database'] := 'DB_MOBILE';
  FDConnection1.Connected := true;
end;

Copy table schema from an existing dataset:

procedure CopyTableSchemaFrom(ADataset: TDataset;
  ATableNm: string);
var
  i: Integer;
  AField: TField;

  procedure L_CopyFieldDefToSQLiteTable(AName: string; aType: TDataType;
    ASize: Integer; AIsRqrd: Boolean);
  var
    LFldSz: Integer;
  begin
    LFldSz:= 0;
    case aType of
      ftString, ftWideString, ftBCD, ftBytes, ftVarBytes, ftBlob, ftMemo, ftGraphic: LFldSz:= ASize;
    end;

    tblSQLite.FieldDefs.Add(AName, aType, LFldSz, AIsRqrd);
  end;

begin
  if ADataset = nil then
    Assert(false, 'Unassigned argument supplied in ADataset.');
  if Trim(ATableNm) = '' then
    Assert(false, 'Empty argument supplied in ATableNm.');

  // SQLite Table name should be same as .DBF file name
  tblSQLite.TableName := ATableNm;

  { Loop through the field in source dataset and copy them to SQLite table. }
  for i := 0 to ADataset.FieldCount - 1 do
  begin
    AField := ADataset.Fields[i];
    if AField = nil then
      Continue;

    L_CopyFieldDefToSQLiteTable(AField.FieldName, AField.DataType,
      AField.DataSize, AField.Required);
  end;

  tblSQLite.CreateDataSet;
end;

Copy value from existing dataset to SQLite;

procedure CopyDataFrom(ASrc: TDataset;
  ASQLiteTblNm: string);
var
  i: Integer;
begin
  if ASrc = nil then
    Assert(false, 'Unassigned argument supplied in ASrc.');
  if Trim(ASQLiteTblNm) = '' then
    Assert(false, 'Empty argument supplied in ASQLiteTblNm.');

  tblSQLite.Close;
  tblSQLite.CachedUpdates := true;
  tblSQLite.Active := true;

  ASrc.First;
  while not ASrc.Eof do
  begin
    tblSQLite.Insert;

    for i := 0 to ASrc.FieldCount - 1 do
    begin
      tblSQLite.Fields[i].Value := ASrc.Fields[i].Value;
    end;

    ASrc.Next;
  end;

  tblSQLite.ApplyUpdates;
  tblSQLite.CommitUpdates;
end;

Aucun commentaire:

Enregistrer un commentaire