【问题标题】:Deploying SQLite database to Android with Delphi FireMonkey使用 Delphi FireMonkey 将 SQLite 数据库部署到 Android
【发布时间】:2014-03-06 19:05:09
【问题描述】:

我正在尝试使用 delphi 部署一个应用程序以在 android 模拟器上运行,该模拟器使用 SQLite 数据库并使用查询结果填充组合框。

我已经在 Win32 应用程序上测试了所有代码,并且一切都按预期工作,但是当我部署 SQLite 数据库并尝试在模拟器上运行应用程序时,我引发了“TDBXError with message”异常,并且 ErrorMessage 包含'没有这样的表:汽车'

下面是我的表单代码。

    var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Populate Manufacturer box
  SQLConnection1.Connected := True;
  SQLQuery1.SQL.Clear;
  SQLQuery1.Close;
  SQLQuery1.SQL.Add('SELECT DISTINCT manufacturer FROM cars');
  try
    SQLQuery1.Open;
    cbManufac.Items.Clear;
    while not SQLQuery1.Eof do
    begin
      cbManufac.Items.Add(SQLQuery1.Fields[0].AsString);
      SQLQuery1.Next;
    end;
  finally
    SQLQuery1.Close;
  end;
end;

procedure TForm1.SQLConnection1BeforeConnect(Sender: TObject);
begin
  {$IF DEFINED(iOS) or DEFINED(ANDROID)}
  SQLConnection1.Params.Values['ColumnMetadataSupported'] := 'False';
  SQLConnection1.Params.Values['Database'] :=
      System.IOUtils.TPath.Combine(TPath.GetDocumentsPath, 'cars.sqlite');
  {$ENDIF}
end;

end.

我已确保将 System.IOUtils 添加到使用中,并将我的数据库文件添加到我的项目部署设置下。

如果我激活 Win32 并测试应用程序,组合框条目就可以添加。

在表单设计器中我使用的是 TSQLConnection 和 TSQLQuery

谁能指出我正确的方向。

谢谢

【问题讨论】:

    标签: android sqlite delphi firemonkey delphi-xe5


    【解决方案1】:

    在 Deployment Manager 中,将数据库的远程路径设置为 assets\external。 (有关assets\internalassets\external 的区别,请参阅documentation here。)

    将您的 BeforeConnect 事件代码更改为:

    procedure TForm1.SQLConnection1BeforeConnect(Sender: TObject);
    begin
      {$IF DEFINED(iOS) or DEFINED(ANDROID)}
      SQLConnection1.Params.Values['ColumnMetadataSupported'] := 'False';
      SQLConnection1.Params.Values['Database'] :=
          TPath.Combine(TPath.GetSharedDocumentsPath, 'cars.sqlite');
      {$ENDIF}
    end;
    

    要查看TPath.GetSharedDocumentsPath 和其他位置的实际位置,请参阅Standard RTL Path Functions Across the Supported Target Platforms

    【讨论】:

    • 我已使用 assets\external 进行了编辑,因为它失败了,您再次删除了我的更改。
    猜你喜欢
    • 2019-01-24
    • 2015-05-21
    • 2014-11-18
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多