2010-08-10 124 views
0

查看标题的解释。在Android Activity中查询SQLite数据库 - 说找不到列,尽管它存在

这里是我使用的方法:

public Cursor getClientByName(String name) throws SQLException { 

    name = name.trim(); 

    Cursor mCursor = 

     mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, KEY_NAME + "=" + name, null, 
       null, null, null, null); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 

    return mCursor; 

} 

这里是调用方法:

list.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long vId) { 

      String name = ((TextView) view).getText().toString(); 

      // Toast.makeText(AvoidForeclosure.this, name, Toast.LENGTH_LONG).show(); 

      Cursor cD = db.getClientByName(name); 
      cD.moveToLast(); 
      int id = cD.getInt(0); 

      Intent intent = new Intent(); 
      intent.setClass(view.getContext(), CurrentMarketValue.class); 
      intent.putExtra("clientId", id); 
      startActivity(intent); 

     } 
     }); 

任何想法,为什么它会抛出“无列中找到与名称= Test“,尽管我的数据库浏览器显示确实存在名为name的列和名为Test的行中的值?

回答

1
public Cursor getClientByName(String name) throws SQLException { 

     name = name.trim(); 

     String q = "SELECT _id, name FROM " + DATABASE_TABLE + " WHERE name=?"; 

     Cursor mCursor = mDb.rawQuery(q, new String[] { name }); 


     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 

     return mCursor; 

    } 

修复此问题。