question

Borisoprit-6789 avatar image
0 Votes"
Borisoprit-6789 asked Borisoprit-6789 edited

Delete all items in Sqlite Table

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 ?

dotnet-xamarindotnet-sqlite
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

jcmanke avatar image
0 Votes"
jcmanke answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Borisoprit-6789 avatar image
0 Votes"
Borisoprit-6789 answered Borisoprit-6789 edited

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();
         }
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Borisoprit-6789 avatar image
0 Votes"
Borisoprit-6789 answered Borisoprit-6789 edited

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>();
             
     
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.