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

标签 android

有人可以为我解释以下问题吗?这是下表创建注意 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/

相关文章:

android - 如何在 Spinner 上添加 float 标签

android - 将垂直彩色线条添加到可绘制的搜索栏背景

android - GCMRegistrar.register 不起作用

android - Android 应用程序包安装在手机上的什么位置

android - 当 BroadcastReceiver 运行时,它在哪个线程上运行,系统范围内还是仅在您的应用程序上运行?

Android Studio 2.0 Preview 升级问题

android - 如何在canvas中记录用户的操作并在android中保存为视频文件?

java - 当 RecyclerView 为空时如何使 SwipeRefreshLayout 工作

Android 4.4.2 & 软键盘。选项菜单是可能的?发生 "Unimplemented WebView method onKeyDown called from"

java - Firebase - Firestore 数据查询调用两次两次