我用以下列创建了一个sql lite数据库:
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中显示它们:
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);
但我收到以下例外:
java.lang.IllegalArgumentException: column '_id' does not exist.
db表没有名为’_id’的列
所以这段代码出了什么问题
谢谢
解决方法:
解决此问题的方法是使用这样的选择语句
选择EmpId作为_id
因为适配器需要一个名为_id的列,如你所说
谢谢