Java.lang.StringIndexOutOfBoundsException: index = 0 length = 0 in get sqlite database

I am trying to open a writeable SQLite database with this code ...

public DataAdapterForServieClass open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

However, I get the following error on the line db = DBHelper.getWritableDatabase();...

06-10 11:58:13.995: ERROR/AndroidRuntime(548): FATAL EXCEPTION: main
06-10 11:58:13.995: ERROR/AndroidRuntime(548): java.lang.StringIndexOutOfBoundsException: index=0 length=0
06-10 11:58:13.995: ERROR/AndroidRuntime(548):     at android.app.ContextImpl.validateFilePath(ContextImpl.java:1518)
06-10 11:58:13.995: ERROR/AndroidRuntime(548):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:725)
06-10 11:58:13.995: ERROR/AndroidRuntime(548):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
06-10 11:58:13.995: ERROR/AndroidRuntime(548):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)

This is the code of my class DBHelper...

static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
            + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }

    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
    }
}   

Maybe someone will help me.

+5
source share
1 answer

When you call getWritableDatabase()for the first time, it will call the following methods, according to the Android documentation ...

onCreate(SQLiteDatabase)
onUpgrade(SQLiteDatabase, int, int)
onOpen(SQLiteDatabase)

You do not have code in the method onCreate()- you need to do something here before it works, for example ...

public void onCreate(SQLiteDatabase database) {
    database.openOrCreateDatabase("/come/example/mydatabase",null);
}

checkDataBase() , , openDatabase() - , , . , - .

, - DBHelper, super(). ...

DBHelper helper = new DatabaseHelper(context);
+2

All Articles