尽管添加了 GreenDao "PRIMARY KEY must be unique" 错误 "myEntity.addIdProperty().autoIncreament()"
GreenDao "PRIMARY KEY must be unique" error happen despite of added "myEntity.addIdProperty().autoIncreament()"
我是 GreenDAO 的新手。这是我的发电机 class
public class FlashCardGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(1, "com.flashcard.model");
Entity lesson = schema.addEntity("Lesson");
lesson.addIdProperty().autoincrement();
lesson.addStringProperty("LessonName");
lesson.addStringProperty("ShortDes");
lesson.addStringProperty("LongDes");
Entity card = schema.addEntity("Card");
card.addIdProperty().autoincrement();
card.addStringProperty("SourceText");
card.addStringProperty("TargetText");
card.addByteArrayProperty("Image");
Property lessonID = card.addLongProperty("lessonID").getProperty();
card.addToOne(lesson, lessonID);
ToMany lessonToCard = lesson.addToMany(card, lessonID);
lessonToCard.setName("cards");
new DaoGenerator().generateAll(schema,"../app/src/main/java");
}
}
尽管我将 id 列添加到 "lesson" 并使其自动增加。当我使用此代码时
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "flashcard-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);DaoSession daoSession = daoMaster.newSession();
LessonDao lessonDao = daoSession.getLessonDao();
Lesson lesson = new Lesson( 1L,"Demo L1", "Nothing", "nothing");
long l = lessonDao.insert(lesson);
if (l > 0) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
} else {
Toast.makeText(getApplicationContext(), "Fail! OMG", Toast.LENGTH_SHORT);
}
第一次运行项目,记录插入成功,第二次运行项目,出现"PRIMARY KEY must be unique"错误。这是暴力主键时的常见错误。但是我也设置了AutoIncreament?可能是因为我将Id设置为1L
? Generated-class不建议修改,Lesson的所有构造函数都是有"Id"param.
好的,很快,在 GreenDao support 上进行了一些搜索后,我找到了解决方案,只需将 ID 参数设置为 NULL,而不是任何 "long number"。成功了。遇到这样的事情后,我会把post留给任何人
插入代码为:
Lesson lesson = new Lesson( null ,"Demo L1", "Nothing", "nothing");
long l = lessonDao.insert(lesson);
我是 GreenDAO 的新手。这是我的发电机 class
public class FlashCardGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(1, "com.flashcard.model");
Entity lesson = schema.addEntity("Lesson");
lesson.addIdProperty().autoincrement();
lesson.addStringProperty("LessonName");
lesson.addStringProperty("ShortDes");
lesson.addStringProperty("LongDes");
Entity card = schema.addEntity("Card");
card.addIdProperty().autoincrement();
card.addStringProperty("SourceText");
card.addStringProperty("TargetText");
card.addByteArrayProperty("Image");
Property lessonID = card.addLongProperty("lessonID").getProperty();
card.addToOne(lesson, lessonID);
ToMany lessonToCard = lesson.addToMany(card, lessonID);
lessonToCard.setName("cards");
new DaoGenerator().generateAll(schema,"../app/src/main/java");
}
}
尽管我将 id 列添加到 "lesson" 并使其自动增加。当我使用此代码时
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "flashcard-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);DaoSession daoSession = daoMaster.newSession();
LessonDao lessonDao = daoSession.getLessonDao();
Lesson lesson = new Lesson( 1L,"Demo L1", "Nothing", "nothing");
long l = lessonDao.insert(lesson);
if (l > 0) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
} else {
Toast.makeText(getApplicationContext(), "Fail! OMG", Toast.LENGTH_SHORT);
}
第一次运行项目,记录插入成功,第二次运行项目,出现"PRIMARY KEY must be unique"错误。这是暴力主键时的常见错误。但是我也设置了AutoIncreament?可能是因为我将Id设置为1L
? Generated-class不建议修改,Lesson的所有构造函数都是有"Id"param.
好的,很快,在 GreenDao support 上进行了一些搜索后,我找到了解决方案,只需将 ID 参数设置为 NULL,而不是任何 "long number"。成功了。遇到这样的事情后,我会把post留给任何人
插入代码为:
Lesson lesson = new Lesson( null ,"Demo L1", "Nothing", "nothing");
long l = lessonDao.insert(lesson);