mercredi 2 septembre 2015

C# SQLite WIndows Universal App Adding data with button click to SQLite db

I've been fiddling with this code. Basically. I have a "Rate from 1-7" and each number is a button. When you click the button, I want it to add the number to the database, under "CurrentMood" (defined as int). However, if I run this app, don't click any of the buttons, close it, and run again, I don't see my data returned. But if I run it, click a button, close it, re-run it, it will give me the last result (which is intended, I want to show the most recent result in my textblock)

I know I'm doing something wrong, I just don't know what.

Here's some code for the buttons

 private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Mood mood1 = new Mood()
        {
        CurrentMood = 1

        };
        SQLite.SQLiteAsyncConnection addconn = new SQLite.SQLiteAsyncConnection("mood2.db");
        await addconn.InsertAsync(mood1);
    }

And this is how I read the last record

public async Task<List<Mood>> GetPosts()
    {
        SQLiteAsyncConnection conn = new SQLiteAsyncConnection("mood2.db");

        var query = conn.Table<Mood>();
        var result = await query.ToListAsync();

        List<String> names = new List<String>();

        foreach (var item in result)
        {
            greetingOutput.Text = "Last time you rated your mood as " + Convert.ToString(item.CurrentMood);

        }


         return result;
    }

Aucun commentaire:

Enregistrer un commentaire