Installing an Existing DB

Shane119

Member
Licensed User
Longtime User
Hi Guys

I know I should know the answer to this, but I don't :( :sign0085:

I can create a SQLite DB on the fly, create tables, add/delete/edit records.. but How do I simply include a DB I've created on the PC in the install package and access it? My brain has gone to sleep and I've ran out of coffee....
 

klaus

Expert
Licensed User
Longtime User
Add your DB with the FileManager in the IDE to the Files folder.
Add following code in the Activity_Create routine:
B4X:
If File.Exists(File.DirInternal,"xxx.db") = False Then
  File.Copy(File.DirAssets, "xxx.db", File.DirInternal, "xxx.db")
End  If
This copies the DB to DirInternal if the DB doesn't yet exist there.
You could replace DirInternal by DirRootExternal.

Best regards.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Add your DB with the FileManager in the IDE to the Files folder.
Add following code in the Activity_Create routine:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Exists([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].DirInternal,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"xxx.db"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]) = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Copy([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].DirAssets,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"xxx.db"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].DirInternal,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"xxx.db"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
This copies the DB to DirInternal if the DB doesn't yet exist there.
You could replace DirInternal by DirRootExternal.

Best regards.

Yep. That is how I do it, also.

Two small additions:
1) I use DirExternal which is supposed to be the SD card if there is one, I think.
2) If your DB is bigger than 1 MB, you will get an error on Android 2.1 and below when it tries to decompress your .db file during copy. Erel recommended using the file extension .jet instead of .db when placing it in the B4A files directory and adding to the project. This suppresses compression and allows the file copy to work. But, it makes the .apk larger.

If your DB is less than 1 MB you don't have to mess around with changing the file extension.

Barry.
 
Upvote 0

Shane119

Member
Licensed User
Longtime User
As always - sound advice :) Thank you very much Klaus and Barry.

PS. I've found a couple of tea bags - so caffeine back on the menu :)
 
Upvote 0

Claus

Member
Licensed User
Longtime User
I can't read the code because there is only the html in the box!?!
Where is the filemanager in the IDE ???
b4a V2.22
Claus
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Careful with checking if db exists. When phone still starting up (that's when you see on status bar, the "scanning media files", the check ifExists, may return a false. I get around this problem, by initiating a timer, which gives a but of time to the phone in case I get a 'false'. If after some time, the return is still a 'false' I copy the db as Klaus suggested.
 
Upvote 0
Top