簡體   English   中英

SQLiteDatabase中的數據插入失敗

[英]Data insertion failed in SQLiteDatabase

我正在嘗試創建一個person對象,並將其保存在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();
    }
}

請嘗試以下方法之一:

  • 確保數據庫名稱和架構正確。
  • 從設備上卸載/清除應用程序的數據,然后再次安裝該應用程序。

卸載原因:

在編碼時,您可能已經更改了數據庫的架構。 但是不幸的是,如果應用程序的版本代碼相同,則在使用已經存在舊模式的其他模式重新安裝應用程序時,將不會更新該模式。 因此,清除應用程序數據將是允許更新新架構的最簡單方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM