2016-11-15 7 views
1

Я делаю приложение в форматах Xamarin, но, что бы я ни пытался, я не могу получить базу данных sqlite. Я хочу выбрать все категории, где menu_ID = 1, как я могу это сделать? Мне нужен этот код внутри другой страницы (CategoriePage.xaml.cs) может помочь мне с этим?Как получить данные из базы данных sqlite с помощью функции?

Вот код, который я использую:

(Tables.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() 
     { 
     } 
    } 
} 

DB помощник (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); 
     } 
    } 

спасибо, что вы dvance,

ответ

2

Я предлагаю вам сделать класс 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);

+0

Thx это, вероятно, будет работать сейчас У меня есть только ошибка базы данных, но я задам новый вопрос. –

+0

Привет, у меня есть один маленький вопрос, как я могу использовать эту функцию для всех имен (имен) категорий, которые я возвращаю? –

+0

return db.Table () .Where (x => x.Name.Equals ("nameOfCategory"). ToList(); это то, что вы ищете? – 476rick

Смежные вопросы