Android SQLite:插入/替换方法中的 nullColumnHack 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2662927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android SQLite: nullColumnHack parameter in insert/replace methods
提问by poke
The Android SDK has some convenience methods for manipulating data with SQLite. However both the insertand replacemethods use some nullColumnHack
parameter which usage I don't understand.
Android SDK 有一些使用 SQLite 操作数据的便捷方法。然而,插入和替换方法都使用了一些nullColumnHack
我不理解的参数。
The documentation explains it with the following, but what if a table has multiple columns that allow NULL
? I really don't get it :/
该文档用以下内容对其进行了解释,但是如果一个表有多个允许NULL
? 我真的不明白:/
SQL doesn't allow inserting a completely empty row, so if initialValues is empty, this column [/row for replace] will explicitly be assigned a
NULL
value.
SQL 不允许插入完全空的行,因此如果 initialValues 为空,则此列 [ /row for replace] 将显式分配一个
NULL
值。
回答by CommonsWare
Let's suppose you have a table named foo
where all columns either allow NULL
values or have defaults.
假设您有一个名为的表foo
,其中所有列都允许NULL
值或具有默认值。
In some SQL implementations, this would be valid SQL:
在某些 SQL 实现中,这将是有效的 SQL:
INSERT INTO foo;
That's not valid in SQLite. You have to have at least one column specified:
这在 SQLite 中无效。您必须至少指定一列:
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.
因此,在您将空传递ContentValues
给的情况下insert()
,Android 和 SQLite 需要一些可以安全分配NULL
给的列。如果您有多个此类列可供选择,请通过您选择的选择机制选择一个:掷骰子、Magic 8-Ball(TM)、硬币翻转、隔间伴侣翻转等。
Personally, I'd've just made it illegal to pass an empty ContentValues
to insert()
, but they didn't ask me... :-)
就个人而言,I'd've只是做是非法的传递一个空ContentValues
来insert()
,但他们没有问我... :-)