Android Question Cannot Open database

catyinwong

Active Member
Licensed User
Longtime User
Found a .db resource from the thread : https://www.b4x.com/android/forum/t...-a-word-is-a-noun-verb-etc.13502/#post-617339, copied to the Direct Assets but then the app says it doesn't exist.

B4X:
Logger connected to:  samsung SM-G6100
--------- beginning of main
Copying updated assets files (2)
** Activity (main) Create, isFirst = true **
Error occurred on line: 52 (Main)
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294): Could not open database
#################################################################
Error Code : 1294 (SQLITE_CANTOPEN_ENOENT)
Caused By : Specified directory or database file does not exist.
    (unknown error (code 1294): Could not open database)
#################################################################
    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:242)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:203)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:518)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:209)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1156)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1101)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:832)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:807)
    at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:44)
    at FordSoft.b4a.AICWords.main._activity_create(main.java:433)
 

npsonic

Active Member
Licensed User
Did you tried to use db from assets? That will not work. You must copy it to DirDefaultExternal, before initializing SQL.

B4X:
If FirstTime Then
    If Not(File.Exists(File.DirDefaultExternal, "wordsdb.db")) Then File.Copy(File.DirAssets, "wordsdb.db",File.DirDefaultExternal, "wordsdb.db")
    SQL1.Initialize(File.DirDefaultExternal, "wordsdb.db", True)
End If

Also I'm not sure how that example should have worked. You can't just swap the database file while it's in use.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Additionally: File.DirAssets is write protected.

When using SQlite (or other db's) there are usually two options:

1. Copy a db like you did

2. Create the db with all tables and insert some basic rows (e.g. CREATE TABLE xxxx IF NOT EXISTS....)

I use option 2 for all of my apps, so I have control over it. Imagine you need to update your app. Copying is then not an option because you would overwrite the existing db and the data in it. By creating, checking if exists and perhaps migrating some data by code you have all options when you update.
 
Upvote 0

catyinwong

Active Member
Licensed User
Longtime User
Did you tried to use db from assets? That will not work. You must copy it to DirDefaultExternal, before initializing SQL.

B4X:
If FirstTime Then
    If Not(File.Exists(File.DirDefaultExternal, "wordsdb.db")) Then File.Copy(File.DirAssets, "wordsdb.db",File.DirDefaultExternal, "wordsdb.db")
    SQL1.Initialize(File.DirDefaultExternal, "wordsdb.db", True)
End If

Also I'm not sure how that example should have worked. You can't just swap the database file while it's in use.
Additionally: File.DirAssets is write protected.

When using SQlite (or other db's) there are usually two options:

1. Copy a db like you did

2. Create the db with all tables and insert some basic rows (e.g. CREATE TABLE xxxx IF NOT EXISTS....)

I use option 2 for all of my apps, so I have control over it. Imagine you need to update your app. Copying is then not an option because you would overwrite the existing db and the data in it. By creating, checking if exists and perhaps migrating some data by code you have all options when you update.

Copied the database to External as npsonic suggested. It worked. Thanks a lot!
 
Upvote 0
Top