Delete all items in Sqlite Table

Borisoprit 371 Reputation points
2021-02-18T21:46:31.63+00:00

I am using Sqlite to save some data and show that again in CollectionView and Entry's .

Have something to delete an item with the id

 //Delete
        public Task<int> DeleteItemAsync(Person person)
        {
            return db.DeleteAsync(person);
        }



 private async void BtnDelete_Clicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                //Get Person

                var person = await App.SQLiteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
                if (person != null)
                {
                    //Delete Person
                    await App.SQLiteDb.DeleteItemAsync(person);
                    txtPersonId.Text = string.Empty;
                    await DisplayAlert("Success", "Person Deleted", "OK");

                }
            }
            else
            {
                await DisplayAlert("Required", "Please Enter PersonID", "OK");
            }
        }

But i want to delete al records in the Table "Person" in ButtonClick.

How do i make this work ?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,122 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-02-19T03:14:51.953+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    But i want to delete al records in the Table "Person" in ButtonClick.

    You could define the DeleteAllItems<T> method in the 'SQLiteHelper' class to add the function.

       public class TodoItemDatabase  
       {  
           readonly SQLiteAsyncConnection _database;  
         
           public TodoItemDatabase(string dbPath)  
           {  
               _database = new SQLiteAsyncConnection(dbPath);  
               _database.CreateTableAsync<TodoItem>();  
           }  
         
           ...  
           public Task<int> DeleteAllItems<T>()  
           {  
               return _database.DeleteAllAsync<TodoItem>();  
           }  
       }  
    

    Then pass the 'Table' parameter to call the DeleteAllItems method to delete the items of a table.

       private async void Button_Clicked(object sender, EventArgs e)  
       {  
           await database.DeleteAllItems<TodoItem>();  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-02-18T22:17:11.78+00:00

    Are you using sqlite-net? They have a DeleteAllAsync<T> method.

    Or you can just execute the SQL statement DELETE FROM <table name> with no where clause.

    0 comments No comments

  2. Borisoprit 371 Reputation points
    2021-02-18T22:36:43.56+00:00

    Thanks for the reply,

    Am using this

    using SQLite;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Scorekaarten
    {
        public class SQLiteHelper
        {
            SQLiteAsyncConnection db;
            public SQLiteHelper(string dbPath)
            {
                db = new SQLiteAsyncConnection(dbPath);
                db.CreateTableAsync<Person>().Wait();
            }
    
            //Insert and Update new record
            public Task<int> SaveItemAsync(Person person)
            {
                if (person.PersonID != 0)
                {
                    return db.UpdateAsync(person);
                }
                else
                {
                    return db.InsertAsync(person);
                }
            }
    
            //Delete
            public Task<int> DeleteItemAsync(Person person)
            {
                return db.DeleteAsync(person);
            }
    
    
    
            //Read All Items
            public Task<List<Person>> GetItemsAsync()
            {
                return db.Table<Person>().ToListAsync();
            }
    
    
    
            //Read Item
            public Task<Person> GetItemAsync(int personId)
            {
                return db.Table<Person>().Where(i => i.PersonID == personId).FirstOrDefaultAsync();
            }
        }
    }
    
    0 comments No comments

  3. Borisoprit 371 Reputation points
    2021-02-19T08:09:22.16+00:00

    Thanks jcmanke and JarvanZhang ,

    Got it working.

     public Task<int> DeleteAllItems<T>()
            {
                return db.DeleteAllAsync<Person>();
            }
    

    //And use it

    private async void BtnDelete_Clicked(object sender, EventArgs e)
            {
    
                await App.SQLiteDb.DeleteAllItems<Person>();
    
    
            }
    
    0 comments No comments