SELECT with SQLITE and Xamarin.Android

Asked

Viewed 212 times

1

I’m trying to run a database check, but it’s not working. I still don’t understand how to do the research based on a criterion, using Sqlite and developing the application using Xamarin.Android.

The function I created is that way:

public Player GetJackpot(string Email) {
        try {
            using (var conn = new SQLiteConnection(Path.Combine(folder, "Player.db"))) {
                var list = conn.Query<Player>("SELECT Jackpot FROM Player WHERE Email = ?", Email);
                return GetJackpot(Email);
            }
        }
        catch (SQLiteException err) {
            Log.Info ("SQLiteEx: ", err.Message);
            return GetJackpot(Email);
        }
    }

And my class is that way:

public class Player
{
    [PrimaryKey, AutoIncrement, Column("Id")]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public int Jackpot { get; set; } // acertando todas as imagens
    public int NoCoins { get; set; }

    public Player() { }
}

I am trying to return the value of the "Jackpot" column according to the "Email" but without success! I don’t know if I need to return some list to later create the query or if something is missing in the code.

  • Can you have more than 1 jackpot per email? or is it unique when you Select with this Where? Because you can at the end of the query user a Singleordefault(). Jackpot and return only the jackpot in string.

  • The jackpot is unique yes, I am incrementing every hit with another function. I will follow your tip to see if it works.

  • Missing quotes for email comparison in your select. It should look like this: "SELECT Jackpot FROM Player WHERE Email = '?'". Tip: Reads [Ask]. Ideally, you should have a [mcve] (this you gave us), declare the problem and the expected behavior. " I’m trying to ... but unsuccessfully" can mean a lot of things ;D

  • I added the tips and details that were missing, but is not yet functional. I do not know what is missing to generate the query.

  • Try using Table method instead of Query: Conn. Table<Player>(). Where(p => p.email.Equals(Email)). Firstordefault();

  • Guys, thanks for the tips! I managed to solve the problem differently and I will post the answer below.

Show 1 more comment

1 answer

0


I was able to solve by changing the function and inserting a foreach, making it easier to scan the query, because the way it was, the return was always null. Goes below:

public string GetJackpot(string Email) {
        try {
            using(var conn = new SQLiteConnection(Path.Combine(folder, "Player.db"))) {
                var result = conn.Query<Player>("SELECT * FROM Player");
                foreach (Player player in result) {
                    if (player.Email.Equals(Email)) {
                        string resp = player.Jackpot.ToString();
                        return resp;
                    }
                }
                return string.Empty;
            }
        } catch (SQLiteException err) {
            Log.Info("SQLiteEx: ", err.Message);
            return null;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.