Android sqlite exception: java.lang.IllegalArgumentException: column '_id' does not exist

I created a sql lite database with the following columns:

static final String dbName="demoDB";
    static final String tableName="Employees";
    static final String colID="EmployeeID";

then

public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE "+tableName+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                colName+" TEXT, "+colAge+" Integer);");
    }

I want to select all the records in the database like this and display them as a grid:

SQLiteDatabase db=this.getWritableDatabase();
         Cursor cur= db.rawQuery("Select "+colName+", "+colAge+" from "+tableName, new String [] {});

String [] from=new String []{DatabaseHelper.colName,DatabaseHelper.colAge};
            int [] to=new int [] {R.id.colName,R.id.colAge};
            SimpleCursorAdapter sca=new SimpleCursorAdapter(this,R.layout.gridrow,c,from,to);


        GridView grid=(GridView)findViewById(R.id.grid);
        grid.setAdapter(sca);

but I get the following exception:

java.lang.IllegalArgumentException: column '_id' does not exist.

There is no column named "_id" in db table

so what's wrong with this code

thank

+3
source share
6 answers

A workaround to this problem is to use a selected status similar to this

select EmpId as _id

forces the adapter to require a column named _id, as you said

thank

+8
source

"_id"

static final String colID="EmployeeID"; 

static final String colID="_id"; 

!

+4

CursorAdapter INTEGER PRIMARY KEY _id ( BaseColumns).

+1

SELECT  _ID as _ID,_ID as _id , cont_type ,.... FROM  DATABASE_TABLE ;

When _ID only ,Message is column '_id' does not exist.
When _id only ,Message is column '_ID' does not exist.
+1

_id ( ) _ID (Uppercase), . _id ( )

0

If you loaded your database from assets: after you changed your id to _id in the base table of the database and loaded it from the folder with files that you first delete, otherwise you will use your old database.

0
source

Source: https://habr.com/ru/post/1754561/


All Articles