Xamarin Forms SQLite Query

Hello everyone!
I'm new to Xamarin and I'm making a application but I'm having some problems with SQLite.
I'll explain the problem first, and I'll add my code below. So basically, I'm able to save a user into my SQLite database but after this, I want to take a part of that user and display it in a label. I'll illustrate:
for example, I save a user with fields:
'Voornaam': Duke
'Achternaam': Decker
....

So now the user is saved, I would like to get the field 'Voornaam' from my table Users where this user is saved. I think I know how to select the name but then I need to convert this object into a string or something. I know it may sound confusing but maybe someone will be able to help me.

Here's some code to give an idea of what I want to do:

My query class:

`public class Ride2EarnDatabase
{
static object locker = new object();
readonly SQLiteAsyncConnection database;
readonly SQLiteConnection db;

    public Ride2EarnDatabase(string dbPath)
    {
        database = new SQLiteAsyncConnection(dbPath);
        database.CreateTableAsync<Rit>().Wait();
        database.CreateTableAsync<Gebruiker>().Wait();
    }

    public Ride2EarnDatabase()
    { }

    public Task<List<Rit>> GetRitAsync()
    {
        return database.Table<Rit>().ToListAsync();
    }

    public Task<int> SaveRitAsync(Rit a)
    {
        if (a.ID != 0)
        {
            return database.UpdateAsync(a);
        }
        else { return database.InsertAsync(a); }
    }

    public Task<int> SaveGebruikerAsync(Gebruiker a)
    {
        if (a.ID != 0)
        {
            return database.UpdateAsync(a);
        }
        else { return database.InsertAsync(a); }
    }

    public Task<List<Gebruiker>> GetGebruikerAsync()
    {
        return database.Table<Gebruiker>().ToListAsync();
    }

    /*public Task<List<Gebruiker>> GetVoornaam()
    {
        return database.QueryAsync<Gebruiker>("SELECT [Voornaam] FROM [Gebruiker]");
    }*/

    /*public IEnumerable<Gebruiker> QueryValuations()
    {
        return db.Query<Gebruiker>("select Voornaam from Gebruiker");

    }*/
}`

My class to convert my object to string (don't even know for sure if this is the way to do it, correct me if I'm wrong):

`public class Business
{
private Task<List> _Voornaam;
private IEnumerable _Test;
private Ride2EarnDatabase _db;

    public Task<List<Gebruiker>> Voornaam
    {
        get { return _Voornaam; }
        set { _Voornaam = value; }
    }

    public IEnumerable<Gebruiker> Test
    {
        get { return _Test; }
        set { _Test = value; }
    }

    public Business()
    {
        _db = new Ride2EarnDatabase();
        //_Voornaam = _db.GetVoornaam();
        //_Test = _db.QueryValuations();
    }

    /*public string VoornaamWeergeven()
    {
        string a = string.Empty;
        Voornaam.
        foreach (Gebruiker b in Voornaam)
        {
            a = b.Voornaam;
        }
        return a.ToString();
    }*/

    public string VoornaamWeergeven()
    {
        string a = string.Empty;
        foreach (Gebruiker b in Test)
        {
            a = b.ToString();
        }
        return a;
    }
}`

I think these are the most important ones, I can add more code if needed.
I would be so grateful if someone would be able to help me!

Answers

  • Hi @FlorianSerneels! Seems that you mixing things in your code and not using MVVM propertly. The model shoud have only properties and getters/setters. In other class you put data access code. Then in view you connect model to data and bind to elements. I recommend to read this article.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @marciosouzajunior said:
    Hi @FlorianSerneels! Seems that you mixing things in your code and not using MVVM propertly. The model shoud have only properties and getters/setters. In other class you put data access code. Then in view you connect model to data and bind to elements. I recommend to read this article.

    Thank you for your Link. But I have more than one Table on Database. How to access any Table using same access method!

Sign In or Register to comment.