我有一个'ImagePendingDBStore'实体,我在这个实体中添加了另外4列(请参见下面的代码中的注释)。在此,一列被索引为“ referenceId”
@Entity(tableName = "ImagePendingDBStore",indices = {@Index(value = "referenceId")})
public class ImagePendingDBStore {
@PrimaryKey(autoGenerate = true)
@NonNull
private int ImageId;
private String referenceId;
private Date createdAt;
private Date updatedAt;
private String imageAbsolutePath;
private int synced = 0;
private String customerFirstName;
private String customerMiddleName;
private String customerLastName;
private String dasId;
private String documentName;
private String imageIdRandom;
private int imageResolution;
//new columns
private String dealerId;
private String lat;
private String lng;
private Date imageCaptureDate;
}
我在数据库类中的迁移逻辑如下:
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN dealerId TEXT");
database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN lat TEXT");
database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN lng TEXT");
database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN imageCaptureDate DATE");
database.execSQL("CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)");
}
};
以下是我面临的错误:
Caused by: android.database.sqlite.SQLiteException: index index_ImagePendingDBStore_referenceId already exists (code 1): , while compiling: CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(index index_ImagePendingDBStore_referenceId already exists (code 1): , while compiling: CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId))
#################################################################
答案 0 :(得分:0)
如消息所述, index_ImagePendingDBStore_referenceId 已经存在。
由于索引存在,所以您只需要4条ALTER语句,即您尚未添加它。由于已经存在,因此添加4列不需要更改索引。
您可以删除以下行:-
database.execSQL("CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)");