2010-07-14 63 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,和你的装载后它从资产文件夹中首先卸载应用程序,否则您将使用您的旧数据库。

相关问题