2014-01-21 43 views
0

我的應用程序將模型名保存在一個字符串中。比它在數據庫中讀取以查找模型。當列不存在時,程序應該採用「NotListed」列。我該怎麼做?Android SQLite找不到的地方

我的代碼:當列不存在但它不起作用時,我認爲遊標爲空。

String s = android.os.Build.MODEL; 
    Cursor dataCursor = mDb.rawQuery 
      ("SELECT * FROM Smartphone WHERE Model = '"+s+"'",null); 
    if (dataCursor!=null) 
      { 
     dataCursor.moveToNext(); 
      }else { 
    dataCursor = mDb.rawQuery 
      ("SELECT * FROM Smartphone WHERE Model = 'NotListed'",null); 
    dataCursor.moveToNext(); 
    } 

回答

1
if (dataCursor!=null) 
    { 
     if(dataCursor.getCount()!=0){ 
      dataCursor.moveToNext(); 
     } 
     else { 
     dataCursor = mDb.rawQuery 
     ("SELECT * FROM Smartphone WHERE Model = 'NotListed'",null); 
     dataCursor.moveToNext(); 
     } 
    } 
6

Cursor是從來沒有null,將只包含0或多行,或拋出異常。

檢查moveToNext()(或更正規的,moveToFirst())的結果移動是否成功,即至少有1行。

-1

嘗試:::: ---- >>>

dataCursor = mDb.rawQuery( 「SELECT * FROM智能手機WHERE模型=」 + 「NotListed」,NULL);