2013-05-05 193 views
0

我正在嘗試更新Android中的sql數據庫。Android SQLite數據庫在更新多個更新時未更新

public boolean updatepasswordbySimcardnumber(String simcard, String password) { 
      mDbHelper = new DatabaseHelper(mCtx); 
      mDb = mDbHelper.getWritableDatabase();   
     Cursor mCursor = null; 
     int retvalue = 0; 

     mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, 
        KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE, KEY_PASSWORD}, 
        null, null, null, null, null); 

     for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){ 
      if(mCursor.getString(2).equals(simcard)){ 
        ContentValues updatevalue = new ContentValues();   
        updatevalue.put(KEY_PASSWORD, password);    
        long colId = mCursor.getColumnIndex(KEY_ROWID); 
        retvalue = mDb.update(SQLITE_TABLE, updatevalue, KEY_ROWID + "=?",new String[] { String.valueOf(colId) });// + colId, null); 
        break; 
      } 
     } 
     mDbHelper.close(); 
     return retvalue > 0; 
    } 

但密碼從未更新。 retvalue總是0.什麼可能是錯誤的? 感謝

回答

2

而不是

long colId = mCursor.getColumnIndex(KEY_ROWID); 

應該

long colId = mCursor.getLong(getColumnIndex(KEY_ROWID)); 
0

其實你做錯了。如果您有多個要插入數據庫的記錄,則必須設置開始事務。

您可以使用數據庫事務這一點。

你怎麼能在Android中使用數據庫事務

If you want to start the transaction there is a method beginTransaction() 
If you want to commit the transaction there is a method setTransactionSuccessful() which will commit the values in the database 
If you had start the transaction you need to close the transaction so there is a method endTransaction() which will end your database transaction 

現在有兩個要點

If you want to set transaction successful you need to write setTransactionSuccessful() and then endTransaction() after beginTransaction() 
If you want to rollback your transaction then you need to endTransaction() without committing the transaction by setTransactionSuccessful(). 

編輯:

EX:

db.beginTransaction(); 
     try { 
      // update table 
      db.setTransactionSuccessful(); 

     }catch { 
      //Error in between database transaction 
     }finally { 
       db.endTransaction(); 

     } 
+0

請參閱我的編輯 – Shrikant 2013-05-05 08:57:56

+0

它解決了您的問題嗎? – Shrikant 2013-05-05 15:57:11