4

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?

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
Martin Moore
  • 733
  • 2
  • 8
  • 21
  • could you paste more code, so we can see the problem? I had a similar one - on windows, i did not see any tables, although they were there, but only in the Form Designer. When i executed the app, then the data was there. – Asped Oct 04 '13 at 16:50
  • Have similar problem only on Android, on Windows is OK. Paths are OK, I checked and the file is visible. Have anyone get it work with TSQLConnection on Android? – Sebastian Xawery Wiśniowiecki Mar 13 '14 at 08:49
  • How do you copy the DB from `assets\internal\` to the documents paths? – mjn Dec 02 '14 at 09:33

5 Answers5

4

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.

FLDelphi
  • 508
  • 7
  • 20
2

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;
Pax Beach
  • 2,059
  • 1
  • 20
  • 27
1

Add uses “System.IOUtils”

In Connecting button:

var
  DbYol : string;
begin

DbYol := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'SqliteDers.s3db';

With FDConnection1 do
begin
LoginPrompt := False;
Params.Clear;
Params.Values['Database'] := DbYol; 
Params.Values['DriverID'] := 'SQLite';
Params.Values['CharacterSet'] := 'utf8'; 
Connected := True; 
// Create new database if no exist
end;

With FDQuery1 do
begin
Active := False; 

Connection := FDConnection1;
SQL.Clear;
SQL.Add( 'CREATE TABLE IF NOT EXISTS DERS (' );
SQL.Add( ' ADI nvarchar(10)');
SQL.Add( ' );');
ExecSQL; 
göre ayarlıyoruz
SQL.Clear;
SQL.Add( 'SELECT');
SQL.Add( ' ADI' );
SQL.Add( ' FROM DERS' );
Active := True;
end;

This is good example with details.

http://www.brsatalay.com/xe5-mobil-uygulamada-sqlite-veritabani-kullanimi

  • 1
    Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Jan 18 '14 at 11:36
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ozan Jan 19 '14 at 20:11
1

I've found that when you make DB changes and deploy those changes, the changes sometimes do not take effect on the device (only tested on ANDROID). Uninstalling the app from the device itself and doing a "Fresh" deploy/install will ensure your changed SQLITE DB gets deployed with the App.

** i.m.o - you should only do this for testing/debugging. I totally agree with @FLDelphi 's post and will be implementing his method in my APP once done ***

Ryno Coetzee
  • 516
  • 4
  • 13
0

I used FireDac .

FDConnection1.Params.Values['Database'] := '$(DOC)/QuickQuote.s3db';

I use this on the FDConnection1BeforeConnect .

All so be sure to include the database file in the Deploment.

I followed this TUT Click Here