7heViking
7heViking

Reputation: 7577

How to create a new row in a SQLite table with the ID column only?

I have a table in an SQLite database on Android that only contains a column with the row ID. This table is used to link datasets in different tables. When I try to insert a new row in the table it throws this exception:

android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO journeyDetailsTable(null) VALUES (NULL)

My table is created like this:

private static final String CREATE_TABLE_JOURNEY_DETAIL = "create table " +
        TABLE_NAME + " (" + ID_COLUMN + " integer primary key autoincrement);";

And I am trying to insert a new row like this:

parentID = db.insert(ANOTHER_TABLE, null, new ContentValues());

Any ideas?

Upvotes: 2

Views: 2279

Answers (2)

Valters Jansons
Valters Jansons

Reputation: 3962

The docs for SQLiteDatabase.insert(String, String, ContentValues) clearly state that SQL doesn't allow inserting a completely empty row without naming at least one column name. This means that if you do not specify any values, you need to specify a column name in which to insert the NULL into. You should be fine with db.insert(ANOTHER_TABLE, ID_COLUMN, new ContentValues()) to accomplish that.

Regardless, you should probably re-think your database structure as having tables set up like this does not seem effective, maintainable and overseeable, especially in the long run.

Upvotes: 2

user3398633
user3398633

Reputation:

Just pass ID_COLUMN as column name in your insert, null isn't a column, but passing null to id will let ID be auto-incremented:

INSERT INTO journeyDetailsTable(ID_COLUMN) VALUES (NULL)

demo

Upvotes: 2

Related Questions