android的sqlite的例外:java.lang.IllegalArgumentException:如果列'_id'不存在不存在、java、sqlite、android

2023-09-09 21:08:46 作者:那抹思念、因你泛滥

我创建了一个精简版的SQL数据库具有以下列:

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";

然后

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

我想在这样的数据库中选择的所有记录,显示在GridView他们:

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.

db表中没有与名称的列_id

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

那么,什么是不对的code

so what is wrong with this code

感谢

推荐答案

一个解决此问题的方法是使用选择说明书像这样

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

选择EMPID作为_id

导致适配器需要名为_id的列如你所说

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

感谢