插入/替换方法中的 nullColumnHack 参数

Android SDK 提供了一些使用 SQLite 操作数据的方便方法。然而,插入更换方法都使用了一些我不理解的 nullColumnHack参数。

文档使用以下内容对其进行了解释,但是如果一个表有多个支持 NULL的列,该怎么办呢?我真的不明白:/

SQL 不允许插入一个完全空的行,因此如果 initialValue 为空,这个列[ /行替换]将被显式分配一个 NULL值。

27436 次浏览

Let's suppose you have a table named foo where all columns either allow NULL values or have defaults.

In some SQL implementations, this would be valid SQL:

INSERT INTO foo;

That's not valid in SQLite. You have to have at least one column specified:

INSERT INTO foo (somecol) VALUES (NULL);

Hence, in the case where you pass an empty ContentValues to insert(), Android and SQLite need some column that is safe to assign NULL to. If you have several such columns to choose from, pick one via the selection mechanism of your choice: roll of the dice, Magic 8-Ball(TM), coin flip, cubicle mate flip, etc.

Personally, I'd've just made it illegal to pass an empty ContentValues to insert(), but they didn't ask me... :-)