Create Table Async

Issue #133 new
Gilberto Freitas created an issue

My problem is that I'm using the CreateTableAsync method and is always returning "0". I researched and saw that this return is a mistake.

I wanted to know what I'm doing wrong.

can you help me?

public class SqliteTable
    {
        private readonly SqliteWrapper _sqliteWrapper;

        public SqliteTable()
        {
            _sqliteWrapper = new SqliteWrapper();
        }

        private async Task<bool> CheckIfExistTable<T>() where T : new()
        {
            var connection = _sqliteWrapper.OpenDatabase();
            try
            {
                var result = await connection.Table<T>().CountAsync();
                return result.Equals(0);
            }
            catch (Exception e)
            {
                Logs.Logs.Error($"Error get count table {typeof(T).Name}: {e.Message}");
                return false;
            }
        }

        public async void CreateTable<T>() where T : new()
        {
            var connection = _sqliteWrapper.OpenDatabase();

            if (await CheckIfExistTable<T>())
            {
                Logs.Logs.Info($"This table {typeof(T).Name} was created");
                return;
            }

            var createTableResult = await connection.CreateTableAsync<T>();
            var value = createTableResult.Results.Values.FirstOrDefault();

            if (value.Equals(1))
            {
                Logs.Logs.Info($"Create table {typeof(T).Name}");
            }
            else
            {
                throw new Exception($"Error create table {typeof(T).Name}");
            }
        }
    }
public class Login
    {
        public Login()
        {
        }

        public Login(string user, string password)
        {
            User = user;
            Password = password;
        }

        public Login(int id, string user, string password)
        {
            Id = id;
            User = user;
            Password = password;
        }

        [PrimaryKey, AutoIncrement, Column("login_id")]
        public int Id { get; set; }

        [Unique, NotNull, Column("login_user")]
        public string User { get; set; }

        [NotNull, Column("login_password")] public string Password { get; set; }
    }
protected override void OnStart()
        {
            try
            {
                var sqliteTable = new SqliteTable();
                sqliteTable.CreateTable<Login>();
            }
            catch (Exception e)
            {
                Logs.Logs.Error($"Error init application: {e.Message}");
            }
        }

Comments (0)

  1. Log in to comment