【问题标题】:Android sqlite if rowid exsist update else insertAndroid sqlite 如果行存在更新否则插入
【发布时间】:2015-12-08 13:18:31
【问题描述】:

我必须通过我的 android 应用程序给 android 应用程序用户评分,一旦我给出评分,它将存储在 sqlite 数据库中,我再次给出相同应用程序的评分,它将再次存储在数据库中,我想要如果应用程序的行 id 已经存在,它将更新表,否则将值插入表中,我知道它非常简单,但对我来说很麻烦,谢谢你的帮助...... 我的代码: 评分栏 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();
    }

}

【问题讨论】:

  • 这里只是插入,更新的代码在哪里?
  • 当然,我尝试了很多但对我不起作用(更新)你只需提供插入的示例代码,否则更新@NigamPatro

标签: android mysql sqlite insert-update


【解决方案1】:

通过运行类似于以下的选择查询来检查 rowid 是否存在:

public boolean rowIdExists(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select 1 from " + TABLE_USERRATE
            + " where row_id=?", new String[] { "" + id });
    boolean exists = (cursor.getCount() > 0);
    cursor.close();
    db.close();
    return exists;
}

然后在您当前的实现中使用它来确定是插入还是更新:

if (rowIdExists(someID)) {
    db.updateuserrate(strA, cxt);
} else {
    db.insertuserrate(strA, cxt);
}

【讨论】:

  • 它工作正常,但所有行值都更新为相同的值我只想更新特定行@bwegs
  • @murugananthamselvam 您的update 查询是什么样的?请在您的问题中发布。
  • 您的代码运行良好,我只是误以为我的更新查询 tanq 寻求您的帮助。@bwegs
  • 如果我的问题是正确的,请点赞我的问题,并保存我 ASK Question block tanq @bwegs
【解决方案2】:

只需尝试更新该行。如果没有找到,插入:

void updateOrInsert(...) {
    SQLiteDatabase db = getWritableDatabase();
    try {
        ContentValues cv = new ContentValues();
        cb.put(...); // all except the ID
        if (db.update(TABLE_USERRATE, cv,
                      KEY_RID + " = " + Str[0], null) == 0) {
            cv.put(KEY_RID, Str[0]);
            db.insert(TABLE_USERRATE, null, cv);
        }
    } finally {
        db.close();
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2014-11-29
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多