使用Delphi中的SQLite数据库丢失数据

时间:2016-05-08 19:41:50

标签: sqlite delphi

我正在尝试在SQLite数据库中保存二进制编码数据,并且我能够保存这些值,但是在保存和关闭数据集之后,很少有字符会丢失。

插入的数据如下所示。

enter image description here

当我在网格或表格中加载已保存的记录时,突出显示的文本会丢失。

创建SQLite连接:

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

从现有数据集复制表架构:

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;

将现有数据集中的值复制到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;

0 个答案:

没有答案
相关问题