我的SQLiteDatabase只有3个条目,UUID,标题和日期工作正常,但自从我添加了更多列后,我收到了这个错误。
不确定它是什么,我已经读过0,-1表示无法读取该列,但我已确保正确拼写所有列名。
public List<Crime> getCrimes() {
List<Crime> crimes = new ArrayList<>();
CrimeCursorWrapper cursor = queryCrimes(null, null);
try {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
crimes.add(cursor.getCrime());
cursor.moveToNext();
}
} finally{
cursor.close();
}
return crimes;
}
public Crime getCrime(UUID id) {
CrimeCursorWrapper cursor = queryCrimes(
CrimeTable.Cols.UUID + " = ?",
new String[] { id.toString() }
);
try {
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return cursor.getCrime();
} finally {
cursor.close();
}
}
private static ContentValues getContentValues(Crime crime) {
ContentValues values = new ContentValues();
values.put(CrimeTable.Cols.UUID, crime.getId().toString());
values.put(CrimeTable.Cols.TITLE, crime.getTitle());
values.put(CrimeTable.Cols.DATE, crime.getDate().getTime());
values.put(CrimeTable.Cols.ACTTYPE, crime.getActType().toString());
values.put(CrimeTable.Cols.PLACE, crime.getPlace().toString());
values.put(CrimeTable.Cols.DURATION, crime.getDuration().toString());
values.put(CrimeTable.Cols.COMMENT, crime.getComment().toString());
return values;
}
private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
CrimeTable.NAME,
null, // Columns - null selects all columns
whereClause,
whereArgs,
null, // groupBy
null, // having
null // orderBy
);
return new CrimeCursorWrapper(cursor);
}
public class CrimeCursorWrapper extends CursorWrapper{
public CrimeCursorWrapper(Cursor cursor) {
super(cursor);
}
public Crime getCrime() {
String uuidString = getString(getColumnIndex(CrimeTable.Cols.UUID));
String title = getString(getColumnIndex(CrimeTable.Cols.TITLE));
long date = getLong(getColumnIndex(CrimeTable.Cols.DATE));
String actType = getString(getColumnIndex(CrimeTable.Cols.ACTTYPE));
String place = getString(getColumnIndex(CrimeTable.Cols.PLACE));
String duration = getString(getColumnIndex(CrimeTable.Cols.DURATION));
String comment = getString(getColumnIndex(CrimeTable.Cols.COMMENT));
Crime crime = new Crime(UUID.fromString(uuidString));
crime.setTitle(title);
crime.setDate(new Date(date));
crime.setActType(actType);
crime.setPlace(place);
crime.setDuration(duration);
crime.setComment(comment);
return crime;
}
}
public class Crime {
private UUID mId;
private String mTitle;
private Date mDate;
private String mActType;
private String mPlace;
private String mDuration;
private String mComment;
public Crime() {
this(UUID.randomUUID());
}
public Crime(UUID id) {
mId = id;
mDate = new Date();
}
public UUID getId() {
return mId;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public String getPhotoFilename() {
return "IMG_" + getId().toString() + ".jpg";
}
public String getActType() {
return mActType;
}
public void setActType(String actType) {
mActType = actType;
}
public String getPlace() {
return mPlace;
}
public void setPlace(String place) {
mPlace = place;
}
public String getDuration() {
return mDuration;
}
public void setDuration(String duration) {
mDuration = duration;
}
public String getComment() {
return mComment;
}
public void setComment(String comment) {
mComment = comment;
}
}
答案 0 :(得分:0)
“CAN”原因是:如果您使用物理移动设备进行调试,在更改代码中的数据库表后,从移动设备中删除应用程序的数据并重新安装apk(或按下调试或运行按钮)。 Android Studio不会自动更新数据库文件。
答案 1 :(得分:0)
添加更多列后是否更改了数据库的版本?这是强制性的。