I don't know where are my mistakes. I am trying to store the lyrics.db
file in my DB_PATH
when the database doesn't exist and my checkDatabase method returns false, or my DB is obsolete.
However, I get the following :-
E/SQLiteLog: (14) cannot open file at line 36356 of [605907e73a] (14) os_unix.c:36356: (2) open(/data/user/0/id.ac.umn.project_uts_mobile_16862/filesid.ac.umn.project_uts_mobile_16862/databases/lyrics.db) - (1) Process ts_mobile_16862 : Pid (12455) Uid (10196) Euid (10196) Gid (10196) Egid (10196) (1) osStat failed "/data/user/0/id.ac.umn.project_uts_mobile_16862/filesid.ac.umn.project_uts_mobile_16862/databases/lyrics.db" due to error (2) (1) osStat failed "/data/user/0/id.ac.umn.project_uts_mobile_16862/filesid.ac.umn.project_uts_mobile_16862/databases" due to error (2) (1) osStat failed "/data/user/0/id.ac.umn.project_uts_mobile_16862/filesid.ac.umn.project_uts_mobile_16862" due to error (2) (1) Stat of /data/user/0/id.ac.umn.project_uts_mobile_16862 : st_mode(40700) st_uid(10196) st_gid(10196) st_ino(265643) (1) Stat of /data/user/0 : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(262147) (1) Stat of /data/user : st_mode(40711) st_uid(1000) st_gid(1000) st_ino(196619) (1) Stat of /data : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(2) E/SQLiteDatabase: Failed to open database '/data/user/0/id.ac.umn.project_uts_mobile_16862/filesid.ac.umn.project_uts_mobile_16862/databases/lyrics.db'. 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:272) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213) at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:701) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:272) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:239) at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1276) at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1231) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:915) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:864) at id.ac.umn.project_uts_mobile_16862.DbHelper.checkDatabase(DbHelper.java:50) at id.ac.umn.project_uts_mobile_16862.DbHelper.createDB(DbHelper.java:85) at id.ac.umn.project_uts_mobile_16862.show_lyrics.onCreate(show_lyrics.java:22) at android.app.Activity.performCreate(Activity.java:7258) at android.app.Activity.performCreate(Activity.java:7249) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:7000) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
it's my code for load the DB
public class DbHelper extends SQLiteOpenHelper { private static String DB_PATH = ""; private static String DB_NAME = "lyrics.db"; private SQLiteDatabase vDatabase; private Context vContext = null; public DbHelper(Context context) { super(context, DB_NAME, null, 1); if( Build.VERSION.SDK_INT >= 17) DB_PATH = context.getFilesDir().getPath()+context.getPackageName()+"/databases/"; else DB_PATH = context.getApplicationInfo().dataDir+"/databases/"; this.vContext = context; } @Override public synchronized void close() { if( vDatabase != null ) { vDatabase.close(); } super.close(); } private boolean checkDatabase() { SQLiteDatabase dbTemp = null; try{ String path = DB_PATH + DB_NAME; dbTemp = SQLiteDatabase.openDatabase(path, null, OPEN_READWRITE); } catch ( SQLiteException e) { } if (dbTemp != null ) { dbTemp.close(); } return dbTemp != null; } public void copyDB() throws SQLiteException{ try { InputStream myInput = vContext.getAssets().open(DB_NAME); String outputFileName = DB_PATH + DB_NAME; Log.d("LIFECYCLE", outputFileName); OutputStream myOutput = new FileOutputStream(outputFileName); byte[] buffer = new byte[1024]; int length; while( (length=myInput.read(buffer)) > 0 ){ myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } catch ( IOException e) { e.printStackTrace(); } } public void openDB() { String path = DB_PATH + DB_NAME; vDatabase = SQLiteDatabase.openDatabase(path, null, OPEN_READWRITE); } public void createDB() { boolean dbExist = checkDatabase(); if ( dbExist ){ } else { this.getReadableDatabase(); copyDB(); } } public List<Lyric> getAllSong(){ List<Lyric> temp = new ArrayList<>(); SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor; try{ cursor = db.rawQuery( "SELECT * FROM lyrics" , null); if( cursor == null) return null; cursor.moveToFirst(); do { Lyric lyric = new Lyric( cursor.getString(cursor.getColumnIndex("index")), cursor.getString(cursor.getColumnIndex("song")), cursor.getString(cursor.getColumnIndex("year")), cursor.getString(cursor.getColumnIndex("artist")), cursor.getString(cursor.getColumnIndex("genre")), cursor.getString(cursor.getColumnIndex("lyrics")) ); temp.add(lyric); } while (cursor.moveToNext()); cursor.close(); } catch ( Exception e){ } db.close(); return temp; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
I've already trying to debug this code for almost 15 hours, and even don't get a single clue, where are my mistakes.
how to check, if that path exist? and how to make directory / path if it didn't exist?