【问题标题】:How to update specific data into SQLite in Xamarin如何在 Xamarin 中将特定数据更新到 SQLite
【发布时间】:2018-08-18 07:15:59
【问题描述】:

我使用 SQLite 创建了一个数据库,用于存储学生的详细信息,包括

Student Name (String)
Student Password (String)
Student ID (String)
Student Attendance(Boolean)

在我的解决方案中,有 3 个视图控制器,如下所述:

ViewController1 : 表格视图,显示学生列表。

ViewController2 :普通视图控制器,带有 3 个文本字段和 1 个切换按钮,用于获取所有 4 个用户详细信息。

ViewController3 : 一个按钮,让用户可以点击,并打开考勤(布尔值)。


我想使用 ViewController3 的按钮更新特定学生(例如 StudentName = Apple)的数据。

要插入新的学生数据,代码应该是这样的: *(默认情况下,出勤为“假”)

void SaveButton_Clicked(object sender, EventArgs e)
{
    using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
    {
        connection.Insert(new Student() { StudentName = AddStuNameTxt.Text, StudentId = AddStuIdTxt.Text, StudentPassword = AddStuPassTxt.Text, StudentAttendence = false});
        new UIAlertView("Updated !", "Student successfully created", null, "OK", null).Show();
    }
}

但是,如果我想在 ViewController3 中将“出勤”(布尔值)更新为 true,其中学生名为“Apple”。我可以知道该怎么做吗?

【问题讨论】:

  • 检查我的更新答案删除 ContectInfo 并将其设为 Student 您的表名。

标签: c# sqlite xamarin xamarin.ios uibutton


【解决方案1】:

使用下面的代码,无论您需要什么,比如按钮点击事件。但我建议您使用 StudentId 而不是 StudentName。

//get specific student record
var studentItem=GetSingleRecord("Apple")
studentItem.Attendance=true; 

//update record afermodification   
UpdateContact(studentItem)

上面使用的方法是这样的。

public Student GetSingleRecord(string Name)
{
  return connection.Table<Student>().FirstOrDefault(i => i.StudentName == Name);
}

public void UpdateContact(Student obj)
{
    connection.Update(obj);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2019-01-21
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多