【问题标题】:How to delete item from SQLite PCL database on Xamarin.Forms?如何从 Xamarin.Forms 上的 SQLite PCL 数据库中删除项目?
【发布时间】:2015-10-30 13:09:10
【问题描述】:

我正在使用有关 SQLite 数据库部署的 Xamarin.Forms 教程之一来学习如何在移动数据库中添加、获取和删除项目。目前,我不太确定如何从表中的数据库项中检索 Id 属性以将其传递给 deleteData 方法。基本上,在我添加一定数量的项目后,我想删除一些数据。我已经在我的 XAML 绑定页面中实现了一个按钮,但我需要知道如何检索某些项目的 Id 属性并将它们传递给我创建的 deleteData 方法。谁能帮我解决这个问题?

代码如下:

StudentDB.cs(数据库类)

using System;
using System.Linq;
using SQLite.Net;
using Xamarin.Forms;
using System.Collections.Generic;

namespace XamarinSqliteSample
{
public class StudentDB
{

    private SQLiteConnection _sqlconnection;

    public StudentDB ()
    {
        _sqlconnection = DependencyService.Get<ISQLite> ().GetConnection ();
        _sqlconnection.CreateTable<Student> ();
    }

    public IEnumerable<Student> GetStudents()
    {
        return (from t in _sqlconnection.Table<Student> ()
                select t).ToList ();
    }

    public Student GetStudent (int id)
    {
        return _sqlconnection.Table<Student> ().FirstOrDefault (t => t.Id == id);
    }

    public void DeleteStudent(int id)
    {
        _sqlconnection.Delete<Student>(id);
    }

    public void AddStudent(Student student)
    {
        _sqlconnection.Insert(student);
    }
}
}

Student.cs(数据库表的项目属性)

using System;
using SQLite.Net.Attributes;

namespace XamarinSqliteSample
{
    public class Student
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }

        public Student ()
        {

        }
    }
}

Register.xaml.cs(用于创建操作数据的方法的 C# 页面)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace XamarinSqliteSample
{
    public partial class Register : ContentPage
    {
        public StudentDB _studentdb;
        public Student student;

        public Register()
        {
            InitializeComponent();
        }

        public void adddata(object s, EventArgs args)
        {
            student = new Student();
            _studentdb = new StudentDB();
            student.Name = name.Text;
            student.Address = address.Text;
            student.Phone = phone.Text;
            student.Id++;
            _studentdb.AddStudent(student);
        }

        public void Showdata(object sender, EventArgs args)
        {
            Navigation.PushModalAsync(new StudentList());
        }

    }
}

StudentList.xaml.cs(用数据库项填充列表视图的 C# 页面)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace XamarinSqliteSample
{
    public partial class StudentList : ContentPage
    {
        public StudentDB _database;
        public Student student;

        public StudentList()
        {
            InitializeComponent();

            _database = new StudentDB();
            var students = _database.GetStudents();
            StudentListView.ItemsSource = students;
        }

        public void deleteData(object s, EventArgs args)
        {
            _database = new StudentDB ();
            _database.DeleteStudent (//Id attribute is passed in here);
        }
    }

【问题讨论】:

    标签: c# database sqlite xaml xamarin


    【解决方案1】:

    这取决于您如何触发deleteData,但您可能希望使用StudentListView.ItemSelected,它应该是您的Student 对象之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 2019-11-10
      • 2023-01-13
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多