-1

以下のコードでは、db.setTransactionSuccessful(); エラー Unreachable code を返します。誰でもこの問題を解決する方法を教えてもらえますか?

public boolean updateDiaryEntry(String title, long rowId)
        {

            ContentValues newValue = new ContentValues();
            newValue.put(Constants.TITLE_NAME, title);

            db.beginTransaction();


            return db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
                new String[]{ Double.valueOf(rowId).toString() })>0;

            db.setTransactionSuccessful();
            db.endTransaction();    

        }
4

2 に答える 2

2

関数を終了する前の行に戻ります。

于 2014-06-06T16:13:52.540 に答える
1

関数から戻った後に2 行のコードがあります。これらの行は、既に関数を離れているため実行されません。これが、到達不能コード メッセージが表示される理由です。return ステートメントの後にコード行を入れたくありません。

return db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
            new String[]{ Double.valueOf(rowId).toString() })>0;   //returned from function on this line

db.setTransactionSuccessful(); //therefore you never get to this line
db.endTransaction();  

代わりに、おそらく次のようなことをしたいと思うでしょう:

db_result = db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
            new String[]{ Double.valueOf(rowId).toString() })>0;

if(db_result){
     db.setTransactionSuccessful(); //Now you can do this conditional on the result of the update
}
db.endTransaction();
return db_result;

データベースの更新結果を格納する変数を作成することにより、関数から戻る前にデータベース関連のクリーンアップ/クローズ関数を実行できます。

于 2014-06-06T16:15:49.937 に答える