Reputation: 155
I am using a sqlite database file in my asset forlder and when I try to open it the error comes up. I don't know how to access it without issues so I can read all the data inside. Inside my database I have one table and I read all the rows in the while cycle. I've tried to use sqliteopenhelper but it didn't work. This is my error stacktrace:
public class Database {
private final static String SELECT_QUERY = "SELECT * FROM ";
/**
* This function is used to get a list of scenes from the database
* @param chapter the chapter to search in the database
* @return the list of scenes of the asked chapter
*/
public static ArrayList<Scene> getChapterScenes(Context context, String chapter){
ArrayList<Scene> sceneList = new ArrayList<>();
DBHelper dbH = new DBHelper(context,"myDB.db",null,1);
SQLiteDatabase sqlData = dbH.getReadableDatabase();
try(Cursor resourceSet = sqlData.rawQuery(SELECT_QUERY + chapter, null)) {
while (resourceSet.moveToNext()) {
System.out.println("Debug: 1234");
sceneList.add(new SceneBuilder().setId(resourceSet.getInt(0))
.setDescription(resourceSet.getString(1))
.setOption(resourceSet.getString(2))
.setNext1(resourceSet.getInt(3))
.setNext2(resourceSet.getInt(4))
.build());
}
}catch (RuntimeException e){
System.out.println("Debug: ERROR");
}
System.out.println("Debug:"+sceneList);
return sceneList;
}
}
E/SQLiteLog: (14) cannot open file at line 37769 of [68b898381a]
(14) os_unix.c:37769: (2) open(//databases/sqlite.db) -
E/SQLiteLog: (1) Process ltenbit.dilemma : Pid (12252) Uid (10437) Euid (10437) Gid (10437) Egid (10437)
(1) osStat failed "//databases/sqlite.db" due to error (2)
(1) osStat failed "//databases" due to error (2)
E/SQLiteLog: (1) Stat of / : st_mode(40755) st_uid(0) st_gid(0) st_ino(2)
E/SQLiteDatabase: Failed to open database 'databases/sqlite.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294 SQLITE_CANTOPEN_ENOENT[1294]): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:300)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:737)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:284)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:251)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1394)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1339)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1001)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:940)
at com.moltenbit.dilemma.data.Database.<init>(Database.java:26)
at com.moltenbit.dilemma.MainActivity.onCreate(MainActivity.kt:14)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.moltenbit.dilemma, PID: 12252
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moltenbit.dilemma/com.moltenbit.dilemma.MainActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294 SQLITE_CANTOPEN_ENOENT[1294]): Could not open database
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294 SQLITE_CANTOPEN_ENOENT[1294]): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:300)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:737)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:284)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:251)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1394)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1339)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1001)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:940)
at com.moltenbit.dilemma.data.Database.<init>(Database.java:26)
at com.moltenbit.dilemma.MainActivity.onCreate(MainActivity.kt:14)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Upvotes: 1
Views: 525
Reputation: 129
You can't access your DB from your local asset folder first you need to create a file in system folder then copy your database in it after that you can access your DB.Actually you have to create a helper class for your sqlite database and do what you need in that class for example check weather database is available or not and functions like opendatabase deletedatabase copydatabase and .... Then use this class any where you need to access your database.
Upvotes: 3