Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android sqlite如果rowid存在更新else插入_Android_Mysql_Sqlite_Insert Update - Fatal编程技术网

Android sqlite如果rowid存在更新else插入

Android sqlite如果rowid存在更新else插入,android,mysql,sqlite,insert-update,Android,Mysql,Sqlite,Insert Update,我必须通过我的android应用程序给android应用程序一个用户评级,一旦我给了评级,它将存储在sqlite数据库中,再次给它将存储在数据库中的同一应用程序评级,我想如果应用程序的行id已经存在,它将更新表,否则将值插入表中,我知道这很简单,但对我来说很麻烦,谢谢你的帮助。。。 我的代码: 分级栏设置分级栏更改侦听器: ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

我必须通过我的android应用程序给android应用程序一个用户评级,一旦我给了评级,它将存储在sqlite数据库中,再次给它将存储在数据库中的同一应用程序评级,我想如果应用程序的行id已经存在,它将更新表,否则将值插入表中,我知道这很简单,但对我来说很麻烦,谢谢你的帮助。。。 我的代码: 分级栏设置分级栏更改侦听器:

   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();
    }

}
ratingBar.setOnRatingBarChangeListener(新ratingBar.OnRatingBarChangeListener(){
公共无效评级已更改(评级杆、浮动评级、,
布尔值(用户){
txtRatingValue.setText(String.valueOf(rating*20)+“rating”);
Log.d(“Raating”,String.valueOf(rating));
strA[21]=字符串的值(额定值);
String userRating=String.valueOf(rating);
curRate=Float.parseFloat(userRating);
DataBaseHelper db=新的DataBaseHelper(Activity1.this);
db.镶齿(strA,cxt);
}
});
公共void inserturerate(字符串Str[],上下文cxt){
//TODO自动生成的方法存根
光标c=null;
字符串strId=“”;
ArrayList userRatepoit=新的ArrayList();
试一试{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
{
value.put(KEY_RID,Str[0]);
value.put(ALL_name,Str[1]);
(所有isbn,Str[2]);
等
价值。投入(所有账面评级,Str[20]);
value.put(所有账簿用户评级,Str[21]);
插入(表_USERRATE,null,值);
Log.d(“插入成功”,表_USERRATE);
//关闭数据库连接
}
试一试{
db.close();
}捕获(例外e)
{
e、 printStackTrace();
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}

通过运行类似于下面的select查询,检查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);
}

只需尝试更新行。如果未找到,请插入:

void updateOrInsert(…){
SQLiteDatabase db=getWritableDatabase();
试一试{
ContentValues cv=新的ContentValues();
cb.put(…);//除ID之外的所有
如果(数据库更新)(表_USERRATE,cv,
键_RID+“=”+Str[0],null)==0){
cv.put(键[0]);
db.insert(表_USERRATE,null,cv);
}
}最后{
db.close();
}
}

这里您只是插入,更新的代码在哪里?当然,我尝试了很多,但没有为我工作(更新)您只需为insert else提供示例代码update@NigamPatroits工作正常,但所有行值都由相同的值更新。我只想更新特定行@bwegs@murugananthamselvam您的
更新
查询是什么样子的?请将其发布在您的问题中。您的代码运行良好,我只是将我的更新查询tanq误认为是您的帮助。@bwegsi如果我的问题正确,请将我的问题向上投票,并将我的问题保存为ASK question block tanq@bwegs