Jump to content
Fudley

Delphi 11, Android, deploy .db file, will not overwrite previous

Recommended Posts

I am trying to deploy a prepared .db file (sqlite) with my app.

 

I have included the file ( fudley.db ) in the deployment list as follows:

 

-local path being the development folder where the app is built (i.e. blank)

-local name is fudley.db

-remote path being "assets\internal\"

-Overwrite set to always

 

the rest of the settings are default.

 

However it is never overwritten. If I add new data to the .db file, and build the APK and run it, the data on the target phone is still the same.  Uninstall the app on the target phone and reinstall = same results.  Clear the apps cache and data on the target phone = no change.

 

Any thoughts?

Share this post


Link to post
2 hours ago, Fudley said:

However it is never overwritten

Overwrite can be somewhat misleading. It refers to what happens when the file is deployed from the local file to the application package, not whether the file is overwritten when the app starts - this process occurs in the System.StartUpCopy unit (in the rtl\common folder of the Delphi source), and as evidenced by line 83 (at least in Delphi 12.1):

  if not FileExists(DestFileName) then //do not overwrite files

..when the files are put in their final destination, if the file exists it is not overwritten. A couple of possible workarounds:

 

1. Patch the System.StartUpCopy unit so that it does overwrite 


2. Use files with "versioned" filenames, e.g. fudley-1.0.0.db, and replace the entry in the deployment with the new version. In your apps startup code, look for files using pattern matching, e.g:

uses
  System.IOUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
  LDBFiles: TArray<string>;
begin
  LDBFiles := TDirectory.GetFiles(TPath.GetDocumentsPath, 'fudley-*.db', TSearchOption.soTopDirectoryOnly);
  // If there's only ever one file, it should be the value in LDBFiles[0], so rename that to fudley.db 
end;

 

Share this post


Link to post

That's some great info - thanks so much! I esp like option #2.

Share this post


Link to post
On 4/5/2024 at 11:34 PM, Fudley said:

However it is never overwritten. If I add new data to the .db file, and build the APK and run it, the data on the target phone is still the same.  Uninstall the app on the target phone and reinstall = same results.  Clear the apps cache and data on the target phone = no change.

 

Any thoughts?

You can configure RAD Studio so that the Run Without Debugging and Run actions completely uninstall any previously-installed version of your application, including the data and cache folders. To do this, before installing the newer version, use the following steps:

  1. Select Run > Parameters.
  2. Enter -cleaninstall in the Parameters field.

Share this post


Link to post

You can put a unit before System.StartUpCopy in uses section and in initialization section of that unit you can decide to delete the old files.

Share this post


Link to post
On 4/5/2024 at 11:34 PM, Fudley said:

Any thoughts?

Try this (don't forget to include the database file in the Deploy) :

 

procedure TForm1.RefreshBD;
var
  PackageName: JString;
  zip: TZipFile;
begin
  PackageName := SharedActivityContext.getPackageResourcePath;
  if TFile.Exists(JStringToString(PackageName)) then
    begin
      TFile.Delete(TPath.GetHomePath + PathDelim + 'myDatabase.s3db');
      zip := TZipFile.Create;
      zip.Open(JStringToString(PackageName), TZipMode.zmRead);
      zip.Extract('assets/internal/myDatabase.s3db', TPath.GetDocumentsPath, False);
      zip.Close;
      zip.free;
    end;
end;

 

Edited by apachx

Share this post


Link to post

Not followed here exactly, but I would always recommend to try to check exactly the same path:

if TFile.Exists(JStringToString(PackageName)) then
    begin
      TFile.Delete(TPath.GetHomePath + PathDelim + 'myDatabase.s3db');

maybe like this, if both "exists" are relevant
 

if TFile.Exists(JStringToString(PackageName)) then
    begin
      
      if TFile.Exists(TPath.GetHomePath + PathDelim + 'myDatabase.s3db') then
      begin
          TFile.Delete(TPath.GetHomePath + PathDelim + 'myDatabase.s3db');
      end;
      ...

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×