我创建了一个包含以下列的 sqlite 数据库:
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);");
}
我想像这样选择数据库中的所有记录并将它们显示在网格视图中:
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.
数据库表没有名称为“_id”的列
那么这段代码有什么问题
Thanks
解决此问题的方法是使用这样的 select 语句
选择 EmpId 作为 _id
因为适配器需要一个名为 _id 的列,如您所说
thanks
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)