Android Question Where is my SQLite DAtabase?

John Morgan

Member
Licensed User
Longtime User
I am developing my first Basic4Android app.

It is going well but during development when I update my SQLite database externally I have to remove the existing database from the file list and add the updated database with a different version name and change the reference to the database in the code. Directly changing the database in the file list does not appear to work.
My questions:
Where is an embedded SQLite database held in my project?
Is my difficulty due to the fact that the database is embedded in the apk file and therefore not accessible?

Best wishes, John M.
 

klaus

Expert
Licensed User
Longtime User
How do you embed the database ?
Where do you copy it ? Becaus you cannot access a database in File.DirAssets.
How did you initailize the database ?
In SQL1.Initialize you entered a Dir and a Filename.
Dir is the directory where the data is stored.
Without seeing your code it's impossible to give a concrete answer.
 
Upvote 0

John Morgan

Member
Licensed User
Longtime User
How do you embed the database ?
Where do you copy it ? Becaus you cannot access a database in File.DirAssets.
How did you initailize the database ?
In SQL1.Initialize you entered a Dir and a Filename.
Dir is the directory where the data is stored.
Without seeing your code it's impossible to give a concrete answer.


Sorry, I thought it there would be a simple answer!

How do you embed the database ?
I used the word 'embed' as I thought that it was the most appropriate way to describe a database which is added into the file list together with the other files for the application and (presumably)loaded with the other programme modules and is not in any way external to the application.

I think I used your examples when I set up the code some time ago.

>>How did you initailize the database ?
In SQL1.Initialize you entered a Dir and a Filename.
Dir is the directory where the data is stored.
Without seeing your code it's impossible to give a concrete answer.<<

I used the code from a code example which I think was yours. I have abstracted the code I think you are looking for:

Dim dbFileName As String : dbFileName="myDataBaseV20.db"
Dim dbFileDirectory As String : dbFileDirectory=File.DirInternal

File.Copy(File.DirAssets, dbFileName,dbFileDirectory,dbFileName)

SQL1.Initialize(File.DirInternal,dbFileName, True)

The application compiles and works as intended. What I am hoping for is, should I update the database externally (using the same file name), that I can then merely copy over the exiting database with the same file name (if I know where it is located in the application's folders) and which the application is currently using. I am trying to avoid the need to amend any database names in the actual code and to avoid having to remove one database name from the file list and to put in a new database with a different name (which I am currently doing). Also to avoid the need to recompile the code (because no names are changed in the code itself).
Best wishes John M.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the example, the database is saved in File.DirInternal. This is a directory dedicated to the application no other application can access it.
What I am hoping for is, should I update the database externally (using the same file name)
This depends on how you want to mange the database.
If you are adding, deleting or updating records in the application the database is directly updated no need for any external intervention. In this case File.DirInternal is the right place.
If you need or want to update the database from somewhere else with another application you should save it in File.DirRootExternal (you can add a subdirectory). There you can acces it with a USB connection, you should use the same name.
If you need to share the database with other devices you should consider the Remote Database Connector.
 
Upvote 0

John Morgan

Member
Licensed User
Longtime User
In the example, the database is saved in File.DirInternal. This is a directory dedicated to the application no other application can access it.This depends on how you want to mange the database.
If you are adding, deleting or updating records in the application the database is directly updated no need for any external intervention. In this case File.DirInternal is the right place.
If you need or want to update the database from somewhere else with another application you should save it in File.DirRootExternal (you can add a subdirectory). There you can acces it with a USB connection, you should use the same name.
If you need to share the database with other devices you should consider the Remote Database Connector.

Thank you Klaus for pointing me in the right direction. Will work on it,

I am grateful for your help,

Best wishes, John M.
 
Upvote 0

RVP

Active Member
Licensed User
Longtime User
I am guessing your code is actually testing for the existence of the database, before copying it, otherwise based on your code, every time you started the app, you would replace the database on the device, with the one in the apk.

Sure you don't have

B4X:
If File.Exists(File.DirDefaultExternal,DbFileName) = False Then
        DBUtils.CopyDBFromAssets(DbFileName)
End If
 
Upvote 0

John Morgan

Member
Licensed User
Longtime User
I am guessing your code is actually testing for the existence of the database, before copying it, otherwise based on your code, every time you started the app, you would replace the database on the device, with the one in the apk.

Sure you don't have

B4X:
If File.Exists(File.DirDefaultExternal,DbFileName) = False Then
        DBUtils.CopyDBFromAssets(DbFileName)
End If


Thanks for that - I do have that in the code and it looks as if I have misunderstood what the File.exists actually does. I will not be able to go back to it for a few days but dealing with that issue should solve my problem.

I do appreciate the help I have received in the Forum,

Best wishes, John M.
 
Upvote 0
Top