mardi 8 décembre 2015

Android sqlite if rowid exsist update else insert

I have to give a user rating to android application through my android applicatiion ,Once i give the rating it will be stored in sqlite database ,again i give the rating for same application it will stored once again in database ,i want if row id of the application already exists it will update the table else insert the values to the table ,i know its very simple but its trouble to me ,thanks for your help... My code : Rating bar setOnRatingBarChangeListener:

   ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
                                    boolean fromUser) {
            txtRatingValue.setText(String.valueOf(rating * 20) + "Rating");
            Log.d("Raating ", String.valueOf(rating));
            strA[21] = String.valueOf(rating);
            String userRating=String.valueOf(rating);
            curRate=Float.parseFloat(userRating);
           DataBaseHelper db = new DataBaseHelper(Activity1.this);
            db.insertuserrate(strA, cxt);

        }
    });
   public  void insertuserrate(String Str[],Context cxt) {

    // TODO Auto-generated method stub
    Cursor c = null;
    String strId="";
    ArrayList<String> userRatepoit= new ArrayList<String>();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
     {
            values.put(KEY_RID, Str[0]);
            values.put(ALL_name, Str[1]);
            values.put(ALL_isbn, Str[2]);
            etc...
            values.put(ALL_book_rating, Str[20]);
            values.put(ALL_book_userrating, Str[21]);
         db.insert(TABLE_USERRATE, null, values);
         Log.d("inserted success", TABLE_USERRATE);
            // Closing database connection
        }
try {

db.close();
}catch(Exception e)
{
e.printStackTrace();
}
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }

}

Aucun commentaire:

Enregistrer un commentaire