2020年12月15日 星期二

【Delphi Multi-Device Application】 SQLite掛載行動裝置的設定

我使用的是FDConnection 連接 SQLite,
首先FDConnection.DriverName選擇 SQLite,Params >  Database 選擇已建置好的SQLLite 資料庫

再配合 FDQuery 將資料取出反應在畫面的物件上,基本Application的架構這樣的設定就可以使用,要掛載到行動裝置需要再做以下的設定
Project > Deployment 設定發佈後SQLite的放置目錄,[Remote Path] 填入 assets\internal\ 
  • Remote Path on iOS Device platform: StartUp\Documents\
  • Remote Path on Android platform: assets\internal\

FDConnection BeforeConnection事件,在連線前變更讀取路徑

uses System.IOUtils;

procedure TForm1.FDConnection1BeforeConnect(Sender: TObject);
begin
{$IF DEFINED(iOS) or DEFINED(ANDROID)}
  FDConnection1.Params.Values['Database'] := TPath.Combine(TPath.GetDocumentsPath, 'WorkShifts.db');
{$ENDIF}
end;

資料庫有使用到中文的部份,記得將 FDConnection.Params.StringFormat設定為 stUnicode,才會在行動裝置上正確的顯示。