【问题标题】:How to Replace data in SQLite Table - Android C# Xamarin如何替换 SQLite 表中的数据 - Android C# Xamarin
【发布时间】:2017-10-21 13:58:23
【问题描述】:

我正在尝试这样做,以便我可以编辑 SQLite 表中的数据,但它给了我和错误。我想要做的是单击“编辑”按钮,并使用警报对话框中的新字符串编辑已显示在 ListView 中的数据。

更新/编辑代码:

public void UpdateNote(String note)
{
    SQLiteDatabase db = this.WritableDatabase;
    db.Replace(DB_TABLE, DB_COLUMN + " = ?", new String[] { note });
    db.Close();
}

这将从警报对话框正按钮调用。

我收到的错误: enter image description here

更新 1:

自定义适配器:

 btnEdit.Click += delegate
            {

                string noteOld = noteList[position];

                mainActivity.UpdateNote(noteOld);
                //reload data
                mainActivity.LoadNoteList();
            };

以上调用 MainActivity:

public void UpdateNote(String noteOld)
        {
            string oldNote = noteOld;
            noteEditText = new EditText(this);
            noteEditText.Text = oldNote;
            noteEditText.SetLines(5);
            noteEditText.SetTextColor(Color.Black);
            noteEditText.SetBackgroundColor(Color.White);
            noteEditText.SetMaxLines(5);
            //noteEditText.SetGravity(Gravity.Top);
            alert = new AlertDialog.Builder(this);
            alert.SetTitle("Edit Note");
            alert.SetMessage("");
            alert.SetView(noteEditText);
            alert.SetPositiveButton("Save", UpdateOkAction);
            alert.SetNegativeButton("Cancel", CancelAction);
            alert.Create();
            alert.Show();

        }

调用MainActivity方法:

 public void UpdateOkAction(object sender, DialogClickEventArgs e)
        {
            string noteNew = noteEditText.Text;
            dbHelper.UpdateNote(oldNote);
            LoadNoteList();
        }

然后转到 DbHelper.cs:

public void UpdateNote(String noteNew)
        {

            SQLiteDatabase db = this.WritableDatabase;
            ContentValues values = new ContentValues();
            values.Put("Note", noteNew); // Column name and value respectively
            db.Update(DB_TABLE, values, DB_COLUMN + " = ?", new String[] { noteNew });
        }

我无法将 noteNew 和 oldNote 都解析到 DbHelper 以替换数据。

【问题讨论】:

    标签: c# android mysql sqlite xamarin


    【解决方案1】:
    public void UpdateNote(String note)
    {    
        SQLiteDatabase db = this.WritableDatabase;
    
        dataToInsert.put(YOUR_COLUMN_NAME_HERE, note); // Column name and value respectively
    
    
        String[] whereArgs = new String[] {String.valueOf(id)};
    
        db.update(DB_TABLE, dataToInsert, "id=?", whereArgs);
    }
    

    【讨论】:

    • 它给出了一个错误说whereArgs does not exist in the current context
    • 您应该更改我评论的部分以适合您的数据库和数据。将"note","note" 替换为您的信息。
    • 它正在更新数据,但仅通过一组String,我将如何使其字符串来自用户输入Alert Dialog?请记住,上面的代码在DBHelper.cs
    • 我编辑我的答案。只需将要更新的字段名称替换为YOUR_COLUMN_NAME_HERE
    • 我如何从 MainActivity 获得String note
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2019-02-07
    • 1970-01-01
    • 2020-04-01
    • 2018-11-26
    • 2017-12-09
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多