2012-06-28 26 views
-1

我有一個只保存一個值的數據庫。它有兩列,分別是「id」和「status」。 id = 1和status = FALSE。當我寫的條件滿意時,我想將我的表更新爲TRUE。SQLite更新函數

System.out.println("!!!!!entered here!!!!!!!!!"); 
    ContentValues cv = new ContentValues(); 
    cv.put("1", "TRUE"); 
    System.out.println("!!!!!entered here!!!!!!!!!");  

    db.update("main", cv, "id = 1", null); 
    System.out.println("??????not entered here??????"); 

它迫使我退出,當然沒有更新我的表。

我試圖檢查一個條件,如果它滿意,它使狀態爲真。如果狀態爲FALSE,我的應用程序將進入CheckScreen,如果它爲TRUE,我的應用程序將不會進入該屏幕。

我也在聽你對這種情況的解決方案。

回答

0

您的含量值:

ContentValues cv = new ContentValues(); 
cv.put("1", "TRUE"); 

應該

ContentValues cv = new ContentValues(); 
    cv.put("status", "TRUE"); 

狀態是要更新的列名。

對於e.g:

DBHelper dbHelper = new DBHelper(context); 
SQLiteDatabase db = dbHelper.getWritableDatabase(); 
ContentValues updateValues = new ContentValues(); 
updateValues.put("status", "TRUE"); 
int updated = db.update(MYDATABASE_TABLE, updateValues, KEY_ID+"="+id, null); 
+0

,它是「狀態」,但我有同樣的錯誤。 - > java.lang.NullPointerException –

+0

發佈您的日誌貓錯誤,我編輯我的帖子基於錯誤日誌 –

+0

你有另一種解決方案提供?我怎麼才能讓用戶第一次進入CheckScreen? –

0

我找到了解決我的問題,我用共享偏好。我的目的是在設備上保持最初是「假」的狀態,當用戶下載應用程序時,輸入密碼,使該狀態爲「真」。

我Intro.class

SharedPreferences mSharedPrefs = getSharedPreferences("xmlFile", MODE_PRIVATE); 

    boolean checkValue = mSharedPrefs.getBoolean("bool", false); 
    if (checkValue) { 
     Intent intent = new Intent(Intro.this, MainScreen.class); 
     startActivity(intent); 
    } else { 
     Intent intent = new Intent(Intro.this, KeyCheckClass.class); 
     startActivity(intent); 
    } 
} 

我KeyCheckClass.class

... 
private boolean check(String str) { 
     // my validation algorithm 
    } 

    SharedPreferences mSharedPrefs = getSharedPreferences("xmlFile", 
      MODE_PRIVATE); 
    SharedPreferences.Editor mPrefsEditor = mSharedPrefs.edit(); 

    mPrefsEditor.putBoolean("bool", true); 
    mPrefsEditor.commit(); 

    return true; 
} 

在我MainScreen.class我的應用程序,使一個真正的開始。現在,輸入一個有效的密碼後,手機不會再次進入KeyCheckClass.class,它首先通過MainScreen.class