0

AndroidでSQLデータベースを更新しようとしています。

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 です。何が問題なのですか? ありがとう

4

2 に答える 2

2

それ以外の

long colId = mCursor.getColumnIndex(KEY_ROWID);

そのはず

long colId = mCursor.getLong(getColumnIndex(KEY_ROWID));
于 2013-05-05T08:55:50.500 に答える
0

実際、あなたは間違っています。データベースに挿入するレコードが複数ある場合は、begin transaction を設定する必要があります。

これには、データベース トランザクションを使用できます。

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

さて、ポイントは大きく分けて2つ

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().

編集:

元。:

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

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

        }
于 2013-05-05T08:56:23.327 に答える