getWritableDatabase()sqlite错误

时间:2015-02-02 13:40:36

标签: java android sqlite

我正在尝试在我的应用程序中创建与sqlite的连接,但是如果我执行应用程序,在行getWritableDatabase中,应用程序强制关闭,我不明白发生了什么。

DBCore类:

public class DBCore extends SQLiteOpenHelper{
    private static final String DB_NAME = "";
    private static final int DB_VERSION = 1;

    public DBCore(Context ctx){
        super(ctx, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table user(code integer primary key, pass text not null);");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table user;");
        onCreate(db);

    }


}

DBConnection类:

public class DBLiteConnection {
    private SQLiteDatabase db;

    public DBLiteConnection(Context ctx){
        DBCore dbcore = new DBCore(ctx);
        db = dbcore.getWritableDatabase();
    }

    public void insert(int code, int pass){
        ContentValues values = new ContentValues();
        values.put("code", code);
        values.put("pass", pass);

        db.insert("user", null, values);
    }

    public void update(int code, int pass){
        ContentValues values = new ContentValues();
        values.put("code", code);
        values.put("pass", pass);

        db.update("user", values, "_code = "+code, null);
    }

    public void delete(int code, int pass){
        ContentValues values = new ContentValues();
        values.put("code", code);
        values.put("pass", pass);

        db.delete("user", "code = "+code, null);
    }

    public List<DBLiteUser> search(){
        List<DBLiteUser> list = new ArrayList<DBLiteUser>();
        String[] columns = new String[]{"code","pass"};
        Cursor cursor = db.query("user", columns, null, null, null, null, null);
        if(cursor.getCount() > 0){
            cursor.moveToFirst();
            DBLiteUser user = new DBLiteUser();
            user.setCode(cursor.getInt(0));
            user.setPass(cursor.getString(1));
            list.add(user);
        }
        return list;
    }
}

强制关闭此行“db = dbcore.getWritableDatabase();”

日志:

02-02 11:35:21.778: E/AndroidRuntime(2593): FATAL EXCEPTION: main
02-02 11:35:21.778: E/AndroidRuntime(2593): Process: com.example.newpointerbi, PID: 2593
02-02 11:35:21.778: E/AndroidRuntime(2593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpointerbi/com.example.newpointerbi.LoadingApp_Activity}: java.lang.StringIndexOutOfBoundsException: length=0; index=0
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread.access$800(ActivityThread.java:142)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.os.Looper.loop(Looper.java:136)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread.main(ActivityThread.java:5118)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at java.lang.reflect.Method.invoke(Method.java:515)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at dalvik.system.NativeStart.main(Native Method)
02-02 11:35:21.778: E/AndroidRuntime(2593): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=0
02-02 11:35:21.778: E/AndroidRuntime(2593):     at java.lang.String.indexAndLength(String.java:584)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at java.lang.String.charAt(String.java:578)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ContextImpl.validateFilePath(ContextImpl.java:2260)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1053)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at com.example.newpointerbi.DBLiteConnection.<init>(DBLiteConnection.java:16)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at com.example.newpointerbi.LoadingApp_Activity.onCreate(LoadingApp_Activity.java:19)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.Activity.performCreate(Activity.java:5275)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 11:35:21.778: E/AndroidRuntime(2593):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
02-02 11:35:21.778: E/AndroidRuntime(2593):     ... 11 more

哪里出错?

1 个答案:

答案 0 :(得分:4)

private static final String DB_NAME = "";

您需要提供一个有效文件名而不是空字符串的数据库文件名。