简体   繁体   English

SQLiteDatabase中的数据插入失败

[英]Data insertion failed in SQLiteDatabase

I am trying to create a person object and want to save that in SQLiteDatabase, but ultimately the data insertion fails. 我正在尝试创建一个person对象,并将其保存在SQLiteDatabase中,但是最终数据插入失败。 Below are the insertion method and its call. 以下是插入方法及其调用。 Can you guys tell me, what problem is happening here? 你们能告诉我,这里发生了什么问题? Also, I am facing problem to save an image and retrieve it. 另外,我在保存图像并检索图像时面临问题。 I have commented out the code to save the image because it was stopping the application. 我已经注释掉了保存图像的代码,因为它正在停止应用程序。

//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 method 保存方式

//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();
    }
}

Try one of the following: 请尝试以下方法之一:

  • Make sure the database name and schema is correct. 确保数据库名称和架构正确。
  • Uninstall/Clear data of your app from the device and install the app again. 从设备上卸载/清除应用程序的数据,然后再次安装该应用程序。

Reason to uninstall: 卸载原因:

While you are coding, you might have changed the schema of the database. 在编码时,您可能已经更改了数据库的架构。 But unfortunately, if the version code of the app is same, upon reinstalling the app with a different schema where old schema already exists, the schema will not be updated. 但是不幸的是,如果应用程序的版本代码相同,则在使用已经存在旧模式的其他模式重新安装应用程序时,将不会更新该模式。 So clearing app data would be the easiest way to allow new schema to be updated. 因此,清除应用程序数据将是允许更新新架构的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM