public long insert (String table, String nullColumnHack, ContentValues values)
Added in
API level 1
Convenience method for inserting a row into the database.
Parameters
table | the table to insert the row into |
---|---|
nullColumnHack | optional; may be null . SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty. |
values | this map contains the initial column values for the row. The keys should be the column names and the values the column values |
Returns
- the row ID of the newly inserted row, or -1 if an error occurred
主要是解释中间那个参数nullColumnHack,这个参数是一个字段名。如果你想插入一条所有字段值都为NULL的记录,必须要指定一个字段名来作为该参数,来告诉数据库,将要插入的这条记录都为NULL值。如果不指定一个字段名为该参数,而是使用NUll的话,这样插入一条完全为NUll的记录,数据库将会报错。
通俗一点来讲,一般来说,数据不允许插入所有字段值都为NULL的记录,但是如果你指定一个字段为nullColumnHack的值,则数据库允许参加所有字段值都为NULL的记录。