SQLite databases behaviors

NJDude

Expert
Licensed User
Longtime User
I've noticed something particular using Android and SQLite databases, maybe someone can give me an explanation.

I have a database which I want to include on an application, if I add it to the assets, it fails to read the tables, if I add it to the DirInternal same result but if I add it to DirExternal then works like a charm, I've read about the "android_metadata" but doing that didn't work either, how am I supposed to distribute an already made database with my app?.

I tried copying the database from the assets to the SD but how do I pause until the copy is done in order to avoid an error when SQL.initialize takes place?, this particular database is read only so putting it on the assets would be no problem.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The assets folder is a logical folder which is mapped to your APK file. The database cannot be opened from this folder.

You can use DBUtils.CopyFromAssetsFolder to copy the database from the assets to the storage card if a storage card exists. Otherwise it will be copied to the internal folder which is fine for databases.

Copying is done in the main thread so your program will not proceed until the file is ready.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Well, I tried it and I see that it attempts to copy because the file size is always ZERO and of course the program fails.

I keep getting this error:

B4X:
An error occurred in sub DBUtils_copyfromassets
(java line: 32)
java.io.IOException

Which is the same error I got before I used DBUtils, I tried it on 2 different phone2, same results.

It seems a problem with permissions <?> because the same happens if I copy the database to the SD card, but, if I manually copy the database to either location it works.


The database is only 4MB.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
It seems the file is too big to be copied:

B4X:
GC_EXPLICIT freed 6258 objects / 338088 bytes in 105ms
startOutput() output 1, stream 1
Enable ALT for speaker
ALT batt temp = 381
Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.ZIPCodes/.main }
Start proc com.ZIPCodes for activity com.ZIPCodes/.main: pid=32568 uid=10074 gids={1015}
close_akm, fd=132
AUDIO_START: start kernel pcm_out driver.
write blocked for 178 msecs, 226 delayed writes, thread 0x155e8
+ProcessAOLC ResetAOLC delta=8384 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OutputStreamProcess::ResetAOLC()
** Activity (main) Create, isFirst = true **
GC_FOR_MALLOC freed 7608 objects / 362144 bytes in 53ms
Compass CLOSE 
GC_FOR_MALLOC freed 11677 objects / 504640 bytes in 35ms
GC_FOR_MALLOC freed 11282 objects / 429736 bytes in 40ms
Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
Data exceeds UNCOMPRESS_DATA_MAX (5208064 vs 4194304)
GC_FOR_MALLOC freed 13668 objects / 642560 bytes in 37ms
dbutils_copydbfromassets (java line: 30)
java.io.IOException
   at android.content.res.AssetManager.readAsset(Native Method)
   at android.content.res.AssetManager.access$700(AssetManager.java:36)
   at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
   at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:320)
   at anywheresoftware.b4a.objects.streams.File.Copy(File.java:309)
   at com.ZIPCodes.dbutils._copydbfromassets(dbutils.java:30)
   at com.ZIPCodes.main._activity_create(main.java:242)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)
   at com.ZIPCodes.main.afterFirstLayout(main.java:83)
   at com.ZIPCodes.main.access$100(main.java:15)
   at com.ZIPCodes.main$WaitForLayout.run(main.java:71)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:144)
   at android.app.ActivityThread.main(ActivityThread.java:4937)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
java.io.IOException
Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@464321c8 (uid=10074 pid=32568)
Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@463f7238
GetAvailableMs
AudioHardware pcm playback is going to standby.
OutputStreamProcess::ResetAOLC()
GetAvailableMs
GetAvailableMs
GC_EXPLICIT freed 2249 objects / 123488 bytes in 71ms
GetAvailableMs
Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
ACTION_BATTERY_CHANGED pluggedType: 2
Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
isUsbConnected() = true
mPlugType = 2
active iface (usb0) reported as added, ignoring
action = psclient.intent.action.usb_status
ACTION_BATTERY_CHANGED
usbCurrentType = 2
Current type is same as previous, return!

Thanks
 
Upvote 0
Top