tags:

views:

44

answers:

3

Hi all

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 in a gridview:

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 receive the following exception:

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

the db table does not have a column with name '_id'

so what is wrong with this code

Thanks

+2  A: 

When using an adapter, your Table must always have the Primary Key Column named as "_id"

Just change

static final String colID="EmployeeID"; 

to

static final String colID="_id"; 

Cheers!

st0le
A: 

CursorAdapters require an INTEGER PRIMARY KEY column named _id (available from BaseColumns).

alex
+1  A: 

Hi

a workaround to this problem is to use select statment like this

select EmpId as _id

cause the adapter requires a column with the name _id as you said

thanks

Mina Samy