c# - 如何使用函数从sqlite数据库中获取数据?

标签 c# android sqlite xamarin xamarin.forms

我正在以 Xamarin 形式制作应用程序,但无论我尝试什么,我都无法使 sqlite 数据库正常工作。我想选择 menu_ID = 1 的所有类别,我该怎么做?我需要在其他页面 (CategoriePage.xaml.cs) 中使用此代码,有人可以帮我解决这个问题吗?

这是我使用的一些代码:

(表.cs):

namespace AmsterdamTheMapV3
{
    public class Categories
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public int Menu_ID { get; set; }
        public string Name { get; set; }

        public Categories()
        {
        }
    }

    public class Places
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public int Categorie_ID { get; set; }
        public string Name { get; set; }
        public Boolean Featured { get; set; }
        public string OpenHours { get; set; }
        public string Info { get; set; }
        public string Images { get; set; }
        public string Phone { get; set; }
        public string Website { get; set; }
        public string Adress { get; set; }

        public Places()
        {
        }
    }

    public class Events
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }

        public Events()
        {
        }
    }
}

数据库助手(TheMapDB.cs):

public class TheMapDB
{
    private SQLiteConnection db;

    public TheMapDB()
    {
        //Getting conection and Creating table  
        db = DependencyService.Get<ISQLite>().GetConnection();
        db.CreateTable<Categories>();
        db.CreateTable<Places>();
        db.CreateTable<Events>();

        var categories = new Categories()
        {
            ID = 1,
            Menu_ID = 1,
            Name = "test"
        };
        db.Insert(categories);   // Insert the object in the database
    }


    public IEnumerable<Categories> GetCategories()
    {
        return (from t in db.Table<Categories>() select t).ToList();
    }

    //Get specific Categorie
    public Categories GetCategorie(int id)
    {
        return db.Table<Categories>().FirstOrDefault(t => t.ID == id);
    }

    //Delete specific Categorie
    public void DeleteCategorie(int id)
    {
        db.Delete<Categories>(id);
    }

    //Add new student to Categorie
    public void AddCategorie(Categories categorie)
    {
        db.Insert(categorie);
    }
}
}

分类页面.xaml.cs:

 public partial class CategoriePage : ContentPage
    {
        static TheMapDB database;
        TheMapDB categorie = new TheMapDB();

        public CategoriePage(String txt)
        {
            InitializeComponent();
            var layout = new StackLayout { Padding = new Thickness(5, 10) };
            this.Content = layout;
            if(txt.Equals("1"))
            {
                txt = "this text is number 1";
                //needed code can't find soluction

            }
            var label = new Label { Text = txt, TextColor = Color.FromHex("#77d065"), FontSize = 20 };
            layout.Children.Add(label);
        }
    }

提前谢谢你,

最佳答案

我建议你用这样的函数创建一个 DAO 类:(或者把这个函数放在 TheMapDB.cs 中)

public List<Category> GetCategoryByID(int menuID)
{
    return db.Table<Category>().Where(x => x.menu_ID == menuID).ToList();
}

然后您可以在您的 DAO 中从任何地方调用此函数。这对我来说似乎是最好的解决方案。 当你把这个函数放在 TheMapDB.cs 类中时,你可以在你的 CategoryPage.xaml.cs 中说:

database.GetCategoryByID(menuID);

关于c# - 如何使用函数从sqlite数据库中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606812/

相关文章:

c# - X >>= N 做什么?

C# 从字符串中移除制表符,制表符标识

android - 方法 drawVertices() 没有在 Android Canvas 上绘制任何东西

python - SQLite3 和 Python 3 - 字符串错误 "no such column"而不是整数

python - 外键约束在 SQLite 上无法正常工作

c# - 当我重定向时 Controller 方法参数为空

c# - OpenCvSharp 仅以所有 OpenCvDll 开头

android - 如何获取自定义适配器 ListView 中的所有项目?

java - 数据库中的数据如何在不同的 Activity 中分配给数组?

sqlite:只读数据库中的临时表/ View ?