I'm trying to use this DBManager
as SQLite database helper. The problem is that it does creates the database but first throws this error. I'm not getting how to create the database without facing this error.
Note : There are similar questions but does not solve this problem. It does not throw write external storage permission exception.
Here's my database helper class :
public class DBManager extends SQLiteOpenHelper {
private static final String TAG = DBManager.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "iap";
// Login table name
private static final String TABLE_STORIES = "stories";
private ArrayList<Story> storiesList = new ArrayList<>();
private Context mContext;
public DBManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
if(checkDataBase()){
Log.d(TAG, "Database exists and readable");
}else {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_STORIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_TITLE + " TEXT,"
+ KEY_PIC + " TEXT,"
+ KEY_EXTERNAL_LINK + " TEXT,"
+ KEY_EXTERNAL_TITLE + " TEXT,"
+ KEY_PIC_CREDIT_LINK + " TEXT,"
+ KEY_PIC_CREDIT_TITLE + " TEXT,"
+ KEY_DATE + " TEXT)";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
}
/**
* Check if the database exist and can be read.
*
* @return true if it exists and can be read, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DATABASE_NAME, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
e.printStackTrace();
}
return checkDB != null;
/*File dbFile = mContext.getDatabasePath(DATABASE_NAME);
return dbFile.exists();*/
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STORIES);
// Create tables again
onCreate(db);
}
}
This is the log (See the last line):
04-13 16:07:12.918 10920-10920/news.inapic.app E/SQLiteLog: (14) cannot open file at line 32456 of [bda77dda96]
04-13 16:07:12.918 10920-10920/news.inapic.app E/SQLiteLog: (14) os_unix.c:32456: (2) open(//iap) -
04-13 16:07:12.919 10920-10920/news.inapic.app E/SQLiteDatabase: Failed to open database 'iap'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
at news.inapic.app.Helpers.DBManager.checkDataBase(DBManager.java:82)
at news.inapic.app.Helpers.DBManager.onCreate(DBManager.java:53)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at news.inapic.app.Helpers.DBManager.getStories(DBManager.java:139)
at news.inapic.app.Acitivity.MainActivity.setupViewPager(MainActivity.java:240)
at news.inapic.app.Acitivity.MainActivity.onCreate(MainActivity.java:209)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at news.inapic.app.Helpers.DBManager.checkDataBase(DBManager.java:82)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at news.inapic.app.Helpers.DBManager.onCreate(DBManager.java:53)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at news.inapic.app.Helpers.DBManager.getStories(DBManager.java:139)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at news.inapic.app.Acitivity.MainActivity.setupViewPager(MainActivity.java:240)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at news.inapic.app.Acitivity.MainActivity.onCreate(MainActivity.java:209)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.app.Activity.performCreate(Activity.java:6679)
04-13 16:07:12.919 10920-10920/news.inapic.app W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.os.Looper.loop(Looper.java:154)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
04-13 16:07:12.920 10920-10920/news.inapic.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-13 16:07:12.921 10920-10920/news.inapic.app D/DBManager: Database tables created