gpt4 book ai didi

android - .SQLiteConstraintException : tabtest. id may not be NULL 的解释

转载 作者:行者123 更新时间:2023-11-30 02:07:15 26 4
gpt4 key购买 nike

有人可以为我解释以下问题吗?这是下表创建注意 id 是 null

   db.execSQL(
"create table tabtest " +
"(id integer null, name text, timage blob )"
);

如果id为null,下面的语句会报错

 contact.setID(Integer.parseInt(cursor.getString(0)));

错误

05-28 22:03:10.826: E/SQLiteDatabase(1861): android.database.sqlite.SQLiteConstraintException: tabtest.id may not be NULL (code 19)

为什么 parsetInt 的 id 不应该为空?

这是下面的方法..如果您需要其他任何东西,请问我。

     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;

}

最佳答案

documentation状态:

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.

可以肯定的是,手动检查该特定列的值是否为空:

if (cursor.isNull(0)) {
contact.setID(null);
} else {
contact.setID(Integer.parseInt(cursor.getString(0)));
}

关于android - .SQLiteConstraintException : tabtest. id may not be NULL 的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com