2010-07-14 28 views
3

我創建了下面列的SQL數據庫精簡版:的Android SQLite的例外:java.lang.IllegalArgumentException異常:列 '_id' 不存在

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

但我收到以下ex ception:

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

db表中沒有名爲「_id」列

那麼,什麼是錯,此代碼

感謝

+0

可能重複的[IllegalArgumentException:列_id'不存在調用SimpleCursorAdaptor時](http://stackoverflow.com/questions/3236203/illegalargumentexception-column-id-does-not-exist-when-call- to-simplecursora) – Pentium10 2010-07-14 08:40:55

+0

僅僅因爲其他線程沒有提供答案並不意味着這個不是重複的。 – 2010-07-15 14:35:10

回答

8

一個解決此問題的方法是使用選擇statment這樣

選擇EMPID作爲_id

導致適配器需要名爲_id的列如你所說

感謝

4

當使用適配器,您的表必須始終將主密鑰列命名爲「_id

只需更改

static final String colID="EmployeeID"; 

static final String colID="_id"; 

乾杯!

1

試試這個方法

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. 
+0

Tanx.be有用。 – 2015-05-07 20:39:26

0

我試過_id(小寫)而不是_ID(大寫)解決了這個問題。確保用_id再次重建DB(小寫)

非常感謝您

0

如果從資產加載數據庫:你已經常德你的ID數據庫中的基表_id,和你的裝載後它從資產文件夾中首先卸載應用程序,否則您將使用您的舊數據庫。

相關問題