Search code examples
xamarinxamarin.formsxamarin.androidxamarin.iosxamarin-android-player

SQlite Update Statement in xamarin forms


I have an SQlite table with 4 Columns in Xamarin app. I've already inserted 3 columns and now i need an update statement to Update 4th column with some values using for loop.

(OR)

Please suggest any better/other method to do the same.


Solution

  • Do you want to update the record in the sqlite DB? If so you can use this model to update the record in the DB.prijem.BCode is PrimaryKey, we set the type of PrimaryKey is int and AutoIncrement, So, if the model's PrimaryKey is not equal zero, this record stored in the DB, we can update this record by the Model.

      readonly SQLiteAsyncConnection _database;
    
        public PrijemDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<Prijem>().Wait();
        }
    
    public Task<int> SavePrijemAsync(Prijem prijem)
        {
            if (prijem.BCode != 0)
            {
                return _database.UpdateAsync(prijem);
            }
            else
            {
                return _database.InsertAsync(prijem);
            }
        }
    

    Here is my model.

    public class Prijem
    {
        [PrimaryKey, AutoIncrement, Unique]
        public int BCode { get; set; }
        public string Name { get; set; }
        public string FirmName { get; set; }
        public string ItemCode { get; set; }
        public string Count { get; set; }
    }
    

    Here is link about how to execute CRUD, you can refer to it.

    https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows

    Here is a demo about it.

    https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/getstarted-notes-database/