SQL problem about finding db

fabero

Member
Licensed User
Longtime User
I create in my pc a simple SQLITE DB.

I add the files and use this code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   '--------------------------------------------------
   'Display layout
   Log (File.ListFiles(File.DirAssets))
   If FirstTime Then
      SQL1.Initialize(File.DirAssets, "ice.sqlite", False)
   End If
.
.
.
.
End Sub


But doesn't work :BangHead:


B4X:
>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
CheckJNI is ON
--- registering native functions ---
Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=net.fabrizioalberti.ice/.main }
Start proc net.fabrizioalberti.ice for activity net.fabrizioalberti.ice/.main: pid=721 uid=10037 gids={}
Shutting down VM
adbd disconnected
NOTE: attach of thread 'Binder Thread #3' failed
GC_EXPLICIT freed 237 objects / 9024 bytes in 343ms
GC_EXPLICIT freed 17 objects / 712 bytes in 143ms
GC_EXPLICIT freed 2 objects / 48 bytes in 185ms
** Activity (main) Create, isFirst = true **
(ArrayList) [emergy.png, ice.bal, ice.jpg, ice.sqlite, images, in-case-of-emergency-ice-icon.png, principale.bal, sounds, webkit]
sqlite returned: error code = 14, msg = cannot open file at source line 25467
sqlite3_open_v2("/data/data/net.fabrizioalberti.ice/files/ice.sqlite", &handle, 2, NULL) failed
main_activity_create (java line: 199)

android.database.sqlite.SQLiteException: unable to open database file
   at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
   at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:29)
   at net.fabrizioalberti.ice.main._activity_create(main.java:199)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:104)
   at net.fabrizioalberti.ice.main.afterFirstLayout(main.java:84)
   at net.fabrizioalberti.ice.main.access$100(main.java:16)
   at net.fabrizioalberti.ice.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)

Any idea? I'm using the android emulator, and next I want to update or query my db.

Tnx.
 

fabero

Member
Licensed User
Longtime User
You cannot open a database that is located in the assets folder. Files in the assets folder are actually located inside the APK file and cannot be randomly accessed.

You can use DBUtils.CopyDBFromAssets to copy the database.

I don't understand.

You are meaning that I can't open directly my db?

So how I can manipulate the db, with update query, or normal query?

Where I need to find every time the db?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is not possible to open a database directly from the assets folder.
You can choose to use DBUtils.CopyDBFromAssets. It copies the database to a writable location, preferably the storage card (if a storage card is available). It returns a string which is the folder that it copied to.
It will only copy the database if it doesn't already exist in the target folder.
 
Upvote 0

fabero

Member
Licensed User
Longtime User
It is not possible to open a database directly from the assets folder.
You can choose to use DBUtils.CopyDBFromAssets. It copies the database to a writable location, preferably the storage card (if a storage card is available). It returns a string which is the folder that it copied to.
It will only copy the database if it doesn't already exist in the target folder.

Ok that command copy the db in a writable location.

I make my changes on db, and next?

The next time i open the app that use that db, i can read the previous update in the db? How I can store which is the folder that it copied the db?
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
When you start your program, you use the CopyDBFromAssets command. If the db does not exist it copies it, if it exists it leaves it alone.

In both cases it will return the target directory, so you can use the copied/modified db that is in the target directory:
B4X:
Target = CopyDBFromAssets (MyDB) ' Target is now the Dir that your database is in
 
Upvote 0

fabero

Member
Licensed User
Longtime User
When you start your program, you use the CopyDBFromAssets command. If the db does not exist it copies it, if it exists it leaves it alone.

Which DB the original or the modified/previously copied?

In both cases it will return the target directory, so you can use the copied/modified db that is in the target directory:
B4X:
Target = CopyDBFromAssets (MyDB) ' Target is now the Dir that your database is in

So Target is also for future change on the DB. There is anyway to store it? Or I can use the function, everytime, to retrieve the folder location?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Which DB the original or the modified/previously copied?

What happens is CopyDBFromAssets only copies the database from assets if it does not exist on the storage card.

So the first time it is used, it copies the db from assets and lets you know where it is so that you can work with the database.

The next time you use the program CopyDBFromAssets will find that the database exists on the storage card, so it will not copy the one from assets - therefore any work you did on the database in your last session is preserved. Even if it finds a database it will still let you know where it is so you can work on it, therefor you should call CopyDBFromAssets each time you run your program to retrieve the directory your database is in.

Hope that is clearer.
 
Upvote 0
Top