SQLiteDatabase 数据插入失败

Data insertion failed in SQLiteDatabase

我正在尝试创建一个人对象并想将其保存在 SQLiteDatabase 中,但最终数据插入失败。下面是插入方法及其调用。你们能告诉我,这里发生了什么问题吗?另外,我在保存和检索图像时遇到问题。我已经注释掉保存图像的代码,因为它正在停止应用程序。

//Insert
long insertPerson(Person person) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    //values.put(ID_FIELD, person.getId());
    values.put(NAME_FIELD, person.getName());
    values.put(DOB_FIELD, person.getDateOfBirth());
    values.put(MOBILE_FIELD, person.getMobile());
    values.put(AGE_FIELD, person.getAge());
    values.put(NEXT_BIRTHDAY_FIELD, person.getNext_birthday());
    //values.put(IMAGE_FIELD, person.getBitmap().compress(Bitmap.CompressFormat.PNG, 0, stream));

    long inserted = db.insert(PERSON_TABLE, null, values);
    db.close();

    return inserted;
}

保存方法

//Save data
public void saveData() {
    int id = 0;
    String name, date_of_birth, mobile, age, next_birthday;
    Bitmap profilePicture;

    //Get data from user
    date_of_birth = tv_DoB.getText().toString();
    name = et_name.getText().toString();
    mobile = et_mobile.getText().toString();
    profilePicture = iv_profile_image.getDrawingCache();

    age = final_year + "years old";
    next_birthday = remaining_months + "months" + remaining_days + "days";

    Person person = new Person(id, name, date_of_birth, mobile, age, next_birthday);

    Toast.makeText(getContext(), person.toString(), Toast.LENGTH_LONG).show();

    databaseHelper = new DatabaseHelper(getContext(), "person_database", null, 1);

    long inserted = databaseHelper.insertPerson(person);
    if (inserted >= 0) {
        Toast.makeText(getContext(), "Data inserted successfully...", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getContext(), "Data insertion failed...", Toast.LENGTH_SHORT).show();
    }
}

尝试以下方法之一:

  • 确保数据库名称和模式正确。
  • Uninstall/Clear 从设备获取您的应用数据并重新安装该应用。

卸载原因:

在编码时,您可能更改了数据库的架构。但不幸的是,如果应用程序的版本代码相同,则在旧架构已存在的情况下使用不同架构重新安装应用程序时,架构将不会更新。因此,清除应用程序数据将是允许更新新架构的最简单方法。