java.lang.StringIndexOutOfBoundsException:指数= 0长度= 0获取SQLite数据库 [英] java.lang.StringIndexOutOfBoundsException: index=0 length=0 in get sqlite database
问题描述
我想这个code。打开一个可写的SQLite数据库...
I am trying to open a writeable SQLite database with this code...
public DataAdapterForServieClass open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
不过我收到以下错误 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)
这是我的 DBHelper
类的code ...
This is the code of my DBHelper
class...
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
}
}
可能有人请帮助我。
Could someone please help me.
推荐答案
当你调用 getWritableDatabase()
首次,它会调用下面的方法,根据在的Android文档 ...
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)
您没有任何code在的onCreate()
方法 - 你需要做的东西在这里之前,将工作,像这样的。 ..
You don't have any code in your onCreate()
method - you'll need to do something here before it will work, such as this...
public void onCreate(SQLiteDatabase database) {
database.openOrCreateDatabase("/come/example/mydatabase",null);
}
看那 checkDataBase()
法在的链接您发布它调用的openDatabase()
- 这样做同样的事情,而且是重要的一块code,你就错过。您需要创建数据库之前,你可以用它做任何事情。
Look at the checkDataBase()
method in the link you posted where it calls openDatabase()
- this does the same thing, and is the important piece of code that you're missing. You need to create the database before you can do anything with it.
另外,还要确保在某处的code,你正在创建你的DBHelper实例,以便它调用超()
方法。像这样...
Also make sure that somewhere in your code you are creating your DBHelper instance, so that it calls the super()
method. Like this...
DBHelper helper = new DatabaseHelper(context);
这篇关于java.lang.StringIndexOutOfBoundsException:指数= 0长度= 0获取SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!