简体   繁体   English

如何用特定行更新SQQLite数据库?

[英]how to update a SQQLite database with particular row?

while updating in database, I'm not getting the id of particular row 在数据库中更新时,我没有得到特定行的ID
here is my code for update Note in database 这是我的代码更新数据库中的注释

public int updateNote(int id, String tittle, String content , String update_at)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Note.COLUMN_TITTLE,tittle);
        values.put(Note.COLUMN_CONTENT,content);
        values.put(Note.COLUMN_TITTLE,update_at);
        return db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);

    }

Please help me. 请帮我。

update returns the number of rows affected and not the id of the row. update返回受影响的行数,而不是该行的ID。
In your case if successful it would return 1. 在您的情况下,如果成功,它将返回1。
About the id you need you already have it. 关于您需要的ID,您已经拥有它。

As mTak outlined, the update method returns the number of rows affected. 如mTak所述, update方法返回受影响的行数。 You can also see that in the documentation . 您也可以在文档中看到它。

If you want your method to return the ID of the row you updated, then return the id that you pass in, since that's how you select the row to update in the first place: 如果要让方法返回更新的行的id ,请返回传递的id ,因为这是您首先选择要更新的行的方式:

public int updateNote(int id, String tittle, String content , String update_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Note.COLUMN_TITTLE,tittle);
    values.put(Note.COLUMN_CONTENT,content);
    values.put(Note.COLUMN_TITTLE,update_at);
    db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);

    return id;
}

In your Activity Class: 在您的活动班级中:

 //*Note is your setterClass  
 //*notesList is your ArrayList or list
 //*position refers your current list position
 //*setNote is a method in Note Class

 private void updateNote(String note, int position) {
        Note n = notesList.get(position);
        // updating note text
        n.setNote(note);

        // updating note in db
        db.updateNote(n);

        // refreshing the list
        notesList.set(position, n);
        mAdapter.notifyItemChanged(position);
    }

In your database class try below code: 在数据库类中,尝试以下代码:

 public int updateNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Note.COLUMN_NOTE, note.getNote());

        // updating row
        return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
                new String[]{String.valueOf(note.getId())});
    }

here i am trying to update single string.i hope this is workig to you.Let me know is this working or not. 在这里我正在尝试更新单个字符串。我希望这对您有用。让我知道这是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM