我想在android sqlite中通過id獲得一個特定的行,並且寫了下面的代碼,但它不返回任何記錄。我寫了一個getAllRecords()
方法,它返回數據庫中的所有記錄。Android在Sqlite中通過Id選擇行?
任何人都可以解釋我犯的錯誤嗎?
public Bank getBankById(int bankId)
{
Cursor cursor=null;
Bank bnk = null;
cursor = this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_ID + "=" + bankId , null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
int id = cursor.getInt(cursor.getColumnIndex(BanksTable.COL_ID));
String name = cursor.getString(cursor.getColumnIndex(BanksTable.COL_NAME));
String url = cursor.getString(cursor.getColumnIndex(BanksTable.COL_IMAGE_URL));
byte[] image = cursor.getBlob(cursor.getColumnIndex(BanksTable.COL_IMAGE));
bnk=new Bank();
bnk.setId(id);
bnk.setImageURL(url);
bnk.setName(name);
bnk.setImageByteArray(image);
}
cursor.close();
}
return bnk;
}
當你得到你的logcat的任何異常加入「自動增量」屬性ID,解決這個問題? – 2011-04-28 10:30:20
其中是BanksTable.COL_ID和表定義? – Selvin 2011-04-28 10:31:16
db.execSQL( 「CREATE TABLE IF NOT EXISTS」 + BanksTable.NAME + 「( 」 \t \t \t \t + BanksTable.COL_ID +「 整數PRIMARY KEY, 」+ \t \t \t \t BanksTable.COL_NAME +「 VARCHAR」 + \t \t \t \t BanksTable.COL_IMAGE_URL + 「VARCHAR」 + \t \t \t \t BanksTable.COL_IMAGE + 「BLOB);」); – UMAR 2011-04-28 11:03:59