4

How do I insert NULL into a column with swift

let myNullableValue: Double? = nil
fmdb.executeUpdate(
  "INSERT INTO myTable(myNullableColumn) SELECT :myNullableValue"
  , withParameterDictionary: [ "myNullableValue": myNullableValue ])
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80

1 Answers1

4

From the FMDB source code at https://github.com/ccgus/fmdb/blob/master/src/fmdb/FMDatabase.m

- (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {

    if ((!obj) || ((NSNull *)obj == [NSNull null])) {
        sqlite3_bind_null(pStmt, idx);
    }
    // ...

one can conclude that a NSNull value is inserted as a SQLite NULL, i.e. the parameter dictionary should be

[ "myNullableValue": NSNull() ]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382