![](/img/trans.png)
[英]Android SQLite java.lang.IllegalArgumentException: column '_id' does not exist
[英]android sqlite exception:java.lang.IllegalArgumentException: column '_id' does not exist
我用以下列創建了一個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的列,如你所說
謝謝
使用適配器時,表必須始終具有名為“ _id
”的主鍵列
只是改變
static final String colID="EmployeeID";
至
static final String colID="_id";
干杯!
試試這種方式
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.
CursorAdapter需要一個名為_id
的INTEGER PRIMARY KEY
列(可從BaseColumns獲得 )。
我試過_id(小寫)而不是_ID(大寫)解決了這個問題。 確保使用_id(小寫)再次重新創建數據庫
非常感謝你
如果從資產加載數據庫:在數據庫基表中將id轉換為_id之后,將其從您首先卸載應用程序的assets文件夾中加載,否則您將使用舊數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.