1

在我的 android 应用程序中,我有一个预定义的数据库(使用 sqlite 浏览器创建,它是 .db 格式),它位于 assets 文件夹中。用户应输入他/她的详细信息(姓名、出生日期、年龄、性别和血统)并单击保存按钮。当用户点击按钮输入的数据应该被插入到 sqlite 数据库中。但是单击保存按钮后,logcat 显示以下错误。

10-22 06:47:47.931: E/SQLiteLog(795): (11) database corruption at line 50741 of [00bb9c9ce4]
10-22 06:47:47.931: E/SQLiteLog(795): (11) database corruption at line 50780 of [00bb9c9ce4]
10-22 06:47:47.941: E/SQLiteLog(795): (11) statement aborts at 3: [INSERT INTO EMPerson(Age,PersonName,BloodGroup,DOB,Gender) VALUES (?,?,?,?,?)] database disk image is malformed
10-22 06:47:47.951: E/DefaultDatabaseErrorHandler(795): Corruption reported by sqlite on database: /data/data/my.easymedi.controller/databases/EasyMediInfo.db
10-22 06:47:47.951: E/DefaultDatabaseErrorHandler(795): deleting the database file: /data/data/my.easymedi.controller/databases/EasyMediInfo.db
10-22 06:47:48.481: E/SQLiteDatabase(795): Error inserting Age=4 Years 8 Months 5 Days  PersonName=swe BloodGroup=A+ DOB=17-02-2009 Gender=Male
10-22 06:47:48.481: E/SQLiteDatabase(795): android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at my.easymedi.db.DBHelper.insertIntoDatabase(DBHelper.java:221)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at my.easymedi.controller.AddNewPerson.onClick(AddNewPerson.java:203)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.view.View.performClick(View.java:4202)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.view.View$PerformClick.run(View.java:17340)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.os.Handler.handleCallback(Handler.java:725)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.os.Looper.loop(Looper.java:137)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at android.app.ActivityThread.main(ActivityThread.java:5039)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at java.lang.reflect.Method.invokeNative(Native Method)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at java.lang.reflect.Method.invoke(Method.java:511)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-22 06:47:48.481: E/SQLiteDatabase(795):  at dalvik.system.NativeStart.main(Native Method)

我的保存按钮代码段是:

case R.id.btnSave:
        personName = etName.getText().toString();
        date_of_birth = tvDOB.getText().toString();
        age = tvAge.getText().toString();

        int selected_rb_ID = genderGrp.getCheckedRadioButtonId();   
        RadioButton rb = (RadioButton) findViewById(selected_rb_ID);
        gender = rb.getText().toString();
        bloodGrp = spiBloodGrp.getSelectedItem().toString();
        String data = personName + date_of_birth + age + gender + bloodGrp;
        Toast.makeText(getApplicationContext(), data , Toast.LENGTH_SHORT).show();

        Person person = new Person();
        person.setName(personName);
        person.setDate_of_birth(date_of_birth);
        person.setAge(age);
        person.setGender(gender);
        person.setBloodGrp(bloodGrp);

        ContentValues values = new ContentValues();
        values.put(COLUMN_PERSON_NAME, person.getName());
        values.put(COLUMN_DOB, person.getDate_of_birth());
        values.put(COLUMN_AGE, person.getAge());
        values.put(COLUMN_GENDER, person.getGender());
        values.put(COLUMN_BLOODGRP, person.getBloodGrp());

        DBHelper dbHelper = new DBHelper(this);
        //dbHelper.createDataBase();
        dbHelper.openDataBase();
        if(dbHelper.insertIntoDatabase("EMPerson", values)){
            Toast.makeText(getApplicationContext(), "Data has been saved successfully", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "Oops ! Try again", Toast.LENGTH_SHORT).show();
        }
        dbHelper.closeDatabase();

        break;

我的 DBhelper 类有一个名为 insertIntoDatabase 的方法。

public boolean insertIntoDatabase(String table, ContentValues cv) {
    try {
        myDatabase.insert(table, null, cv);
        Log.d("INSERT", "Information Saved");
        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("INSERT", e.toString());
        return false;
    }
}

谁能解释一下发生了什么,我该如何解决这个问题?

谢谢

4

0 回答 0