在Sqlite表中插入null

the*_*itz 36 sqlite android

我想知道如何在Android中的SQLite表中插入一个空值.

这是表:

"create table my table (_id integer primary key autoincrement, " +
                                               "deviceAddress   text not null unique, " +
                                               "lookUpKey text , " + 
                                               "deviceName text , " +
                                               "contactName text , " +
                                               "playerName text , " +
                                               "playerPhoto blob " +
                                               ");";    
Run Code Online (Sandbox Code Playgroud)

我想使用一个简单的插入命令,execSQL但因为其中一个值是blob我不能这样做(我认为).

所以,我正在使用标准db.Insert命令.如何使其中一个值为null?

如果我只是在ContentValues对象中跳过它会自动在列中放置一个空值吗?

小智 30

是的,您可以跳过ContentValues中的字段,db.insert会将其设置为NULL.你也可以用另一种方式:

ContentValues cv = new ContentValues();
cv.putNull("column1");
db.insert("table1", null, cv);
Run Code Online (Sandbox Code Playgroud)

这个directrly将"column1"设置为NULL.您也可以在update语句中使用这种方式.

  • 你不能发送null to put方法,而是必须使用putNull方法 (61认同)
  • 实际上答案并没有错.当您检查位于android.content包中的ContentValues.java的源代码时,可以看到; 该方法只使用提供的null参数调用put方法.http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/content/ContentValues.java#ContentValues.putNull%28java.lang.String %29 (13认同)
  • **public void putNull(String key){mValues.put(key,null); }**...我认为putNull的实现可能会改变,所以使用它而不是直接使用它可能更好.尽管如此,它仍然是一个正确的答案. (6认同)