【问题标题】:How to get data out of sqlite database by using a function?如何使用函数从 sqlite 数据库中获取数据?
【发布时间】:2016-11-15 09:56:14
【问题描述】:

我正在 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);
    }
}
}

CategoriePage.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);
        }
    }

提前谢谢你,

【问题讨论】:

    标签: c# android sqlite xamarin xamarin.forms


    【解决方案1】:

    我建议你用这样的函数创建一个 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);

    【讨论】:

    • 谢谢,现在这可能会起作用我只有一个数据库错误,但我会为此提出一个新问题。
    • 嗨,我有一个小问题,我该如何使用这个函数来返回所有类别的名称(名称)?
    • return db.Table().Where(x => x.Name.Equals("nameOfCategory").ToList(); 是你在找的时候吗?
    • 不,我的意思是这样的: var categories = App.Database.GetCategoryByMenuID(1); listView.ItemsSource = categories.Name; (顺便说一句,这不起作用)所以我可以将相同的功能用于不同的结果。
    • 对不起,你的问题对我来说不是很清楚。也许你应该问一个问题并评论一个链接。
    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多