Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying SQLite database to Android with Delphi FireMonkey

I am trying to deploy an application to run on the android emulator using delphi that uses an SQLite database and populates a combobox with the query results.

I Have tested all the code on a Win32 application and everything is working as intended, however when i deploy the SQLite database and try to run the application on the emulator i raise an exception with "TDBXError with message" and the ErrorMessage contains 'no such table: cars'

Below is the code for my form.

    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.

I Have made sure System.IOUtils is added to uses and my database file is added under my projects deployment settings.

If i activate Win32 and test the application the combobox entries are added just fine.

On the form designer i am using TSQLConnection and TSQLQuery

Can anybody point me in the right direction.

Thanks

like image 618
amit2k5 Avatar asked Oct 19 '25 08:10

amit2k5


1 Answers

In the Deployment Manager, set your remote path for your database to assets\external. (See the documentation here for the difference between assets\internal and assets\external.)

Change your BeforeConnect event code to:

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;

To see the physical location of TPath.GetSharedDocumentsPath and other locations, see Standard RTL Path Functions Across the Supported Target Platforms.

like image 105
Ken White Avatar answered Oct 20 '25 23:10

Ken White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!