Jezer Crespo
Jezer Crespo

Reputation: 2172

Android SQLite "0 _id" in the select statement

I'm using the SQLite-asset-helper by jgilfelt

I try to look into his sample project and came across this code:

    public Cursor getEmployees() {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"0 _id", "FirstName", "LastName"}; 
    String sqlTables = "Employees";

    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null,
            null, null, null);

    c.moveToFirst();
    return c;

}

What's the meaning of "0" in that select statement for _ID field?

Upvotes: 2

Views: 873

Answers (2)

VM4
VM4

Reputation: 6499

Some Android adapters require you to always have an _id field selected even if your table doesn't have one. Try it, your app will crash without selecting an _id. So they are doing a workaround for this by selecting a mock _id value that has 0 in it.

EDIT: A better way to do this would be for example if you want to use "id" instead of "_id" in your tables, just do a SELECT id as _id FROM ...

Upvotes: 1

Szymon
Szymon

Reputation: 43023

It is returning the value of 0 with the column name _id (it's using column name alias).

You can return a column that doesn't exist in your table this way.

Upvotes: 2

Related Questions