Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What path is SQLite database deployment to Android device?

Having followed the DocWiki, I'm deploying my SQLite DB to assets\internal\

and used the following code :

SQLConnection1.Params.Values['Database'] := 
TPath.Combine(TPath.GetDocumentsPath, 'myDB.db');

However, when I try and access one of the tables it doesn't exist.

What is the correct setup for deployment/access for SQLite?

like image 580
Martin Moore Avatar asked Feb 16 '23 06:02

Martin Moore


2 Answers

I just went through deployment of an SQLite database to an android app. Here's what I've learned.

Instad of deploying my database with the app, I create it on connect and then create the tables if they don't exist.

I also use the TFDConnection component instead of the TSQLConnection component.

So on TFDConnection BeforeConnect:


  {$IF DEFINED(IOS) or DEFINED(ANDROID)}

  FDConnection1.Params.Values['Database'] :=
    TPath.GetDocumentsPath + PathDelim + 'MyDatabase.s3db';

  {$ENDIF}

And on TFDConnection AfterConnect:


FDConnection1.ExecSQL('CREATE TABLE IF NOT EXISTS MyTable (myField1 TEXT NOT NULL)');

I just tested this method on the emulator, and my Droid X.

Also make sure you're including the TFDGUIxWaitCursor, and TFDPhysSQLiteDriverLink components.

like image 110
FLDelphi Avatar answered Mar 15 '23 11:03

FLDelphi


Correct set of Deployment (All configurations - Android Platform):

Local Name: myDB.db
Remote Path: .\assets\internal\
Remote Filename: myDB.db

Event BeforeConnection have to cantain follow code:

procedure TDM.conSQLiteBeforeConnect(Sender: TObject);
var
    dbPath: string;
begin
{$IF DEFINED(iOS) or DEFINED(ANDROID)}
    dbPath := TPath.Combine(TPath.GetDocumentsPath, 'myDB.db');
{$ENDIF}
    FDConnection1.Params.Values['Database'] := dbPath;
end;
like image 20
Pax Beach Avatar answered Mar 15 '23 10:03

Pax Beach