有人可以为我解释以下问题。这是下表创建注释,其中id为null
db.execSQL(
"create table tabtest " +
"(id integer null, name text, timage blob )"
);
如果id为null,则在下面的陈述中有错误
contact.setID(Integer.parseInt(cursor.getString(0)));
Erorr
05-28 22:03:10.826: E/SQLiteDatabase(1861): android.database.sqlite.SQLiteConstraintException: tabtest.id may not be NULL (code 19)
为什么对于parsetInt,id应该不为null?
这是下面的方法..如果你还需要什么,请问我。
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM tabtest ORDER BY name";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("drop table tabtest");
db.execSQL(
"create table tabtest " +
"(id integer not null, name text, timage blob )"
);
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setImage(cursor.getBlob(2));
// Adding contact to list
System.out.println (contact);
contactList.add(contact);
} while (cursor.moveToNext());
}
db.close();
// return contact list
return contactList;
}
答案 0 :(得分:1)
结果以及此方法是否在列时抛出异常 value为null或列类型不是字符串类型 实现定义的。
要确保,请手动检查该特定列的值是否为空:
if (cursor.isNull(0)) {
contact.setID(null);
} else {
contact.setID(Integer.parseInt(cursor.getString(0)));
}