6

According to the SQLiteDatabase API documentation:

  • insert(String table, String nullColumnHack, ContentValues values)

    Convenience method for inserting a row into the database.

What does nullColumnHack means?

Can you give me an example?

Ethyl Casin
  • 791
  • 2
  • 16
  • 34
  • 1
    I'm voting to close this question as off-topic because it is a duplicate of many many questions,hence shows lack of research. – Elltz Sep 25 '15 at 04:07
  • API NOTE is pretty clearly at [SQLiteDatabase](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) – belle tian Sep 25 '15 at 04:02

2 Answers2

17

Sometimes you want to insert an empty row, in that case ContentValues have no content value, and you should use nullColumnHack.

For example, you want to insert an empty row into a table student(id, name), which id is auto generated and name is null. You could invoke like this:

ContentValues cv = new ContentValues();
db.insert("student", "name", cv);
Ted Yu
  • 1,784
  • 1
  • 10
  • 16
  • What happen when I specified the nullColumnHack and what happen If I dont? – Ethyl Casin Sep 25 '15 at 04:02
  • @EthylCasin I guess, if you specify nullColumnHack, the final SQL statement will have a null value for that column. – Ted Yu Sep 25 '15 at 04:05
  • What does that mean? – Ethyl Casin Sep 25 '15 at 04:05
  • 1
    @EthylCasin In the example above, android framework generates such a SQL statement: "insert into student(name) values(null)", if you dont, android framework have no idea what to fill into that statement. – Ted Yu Sep 25 '15 at 04:07
9

Did you try looking at the docs?

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.

Buddy
  • 10,874
  • 5
  • 41
  • 58