我有一個遊標問題。 我在我的DB中有3張桌子:製作(指發票),車輛(車輛)和車庫。Android:使用光標從數據庫設置Spinner的選定項目
發票涉及一輛車和一個車庫。當我創建發票時,我從紡紗工人那裏選擇車輛和車庫。
當我想更新發票時,我需要設置在這些紡紗器中選擇的項目。
這裏是我該怎麼做:
for (int iVhc = 0; iVhc < spListeVhc.getCount(); iVhc++) {
Cursor valueVhc = (Cursor) spListeVhc.getItemAtPosition(iVhc);
long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id"));
long idSpVhc = valueVhc.getLong(valueVhc.getColumnIndex("TB_VEHICULE._id"));
if (idSpVhc == idVhc) {
spListeVhc.setSelection(iVhc);
}
}
for (int iGar = 0; iGar < spListeGar.getCount(); iGar++) {
Cursor valueGar = (Cursor) spListeGar.getItemAtPosition(iGar);
long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id"));
long idSpGar = valueGar.getLong(valueGar.getColumnIndex("TB_GARAGE._id"));
if (idSpGar == idGar) {
spListeGar.setSelection(iGar);
}
}
它適用於車庫,但問題是,對於我不明白一個道理,車輛的微調採用相同的ID比車庫。
也就是說,如果選擇的車庫在DB中具有ID 2,則所選車輛也將是具有ID 2的車輛。
??
這裏是我的查詢來獲取發票:
public Cursor recupFacture(long idFacture){
return db.rawQuery("SELECT TB_FACTURE._id, libelle_fa, date_fa, nom_vhc, kilometrage_fa, nom_garage, remarque_fa, date_paie_fa, montant_fa, TB_VEHICULE._id, TB_GARAGE._id" +
" FROM TB_FACTURE, TB_VEHICULE, TB_GARAGE" +
" WHERE fk_vhc_fa = TB_VEHICULE._id" +
" AND fk_gar_fa = TB_GARAGE._id" +
" AND TB_FACTURE._id ="+idFacture, null);
}
我意識到,我有一種THI在我的日誌錯誤:
08-10 12:54:22.431: ERROR/Cursor(17072): requesting column name with table name -- TB_VEHICULE._id
而同爲車庫...
感謝您的幫助!
編輯:
我找到了解決辦法。
我更換了TB_GARAGE._id和TB_VEHICULE._id由FK在行:
long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id"));
long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id"));
不過,我真的不能解釋爲什麼它是這樣工作的,但不能與ID。 表的前綴會導致一個奇怪的錯誤...
這個錯誤來自我寫TB.GARAGE/VEHICULE._id 這個事實如果我只寫_id,我有同樣的問題,但在logcat中沒有更多的錯誤。 – HerrM