I have following class "Singleton" to handle SQLite connection and to make sure to have 1 instance of connection for whole process/app:
public class DBController {
private static DBController instance = new DBController();
private static DBHelper dbHelper;
public static DBController getInstance()
{
return instance;
}
public SQLiteDatabase dbOpen(Context context)
{
if(dbHelper == null)
dbHelper = new DBHelper(context);
return dbHelper.getWritableDatabase();
}
}
And DBHelper class itself:
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "database.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String position = "CREATE TABLE test (" +
"test TEXT NOT NULL);";
db.execSQL(position);
}
}
When I frequently try to "SELECT" some info from database I am receiving following error:
SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) -
SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) -
SQLiteLog: (14) statement aborts at 29: [SELECT * FROM test WHERE test='testdata1'] unable to open database file
SQLiteQuery: exception: unable to open database file (code 14); query: SELECT * FROM test WHERE test='testdata1'
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
I am running following code to execute query:
public String getData(Context context)
{
SQLiteDatabase _db = dbOpen(context);
Cursor c = _db.rawQuery("SELECT * FROM test WHERE test='testdata1'", null);
return getDataFromCursor(c).get(0); //gets data from cursor and returns first one
}
How can I manage/improve my database connection to overcome/avoid this problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…