MilkBottle
MilkBottle

Reputation: 4342

How to reset the Primary key and delete the SQLite Db created

This code will create the Database in : LocalFolder

string DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "customers.sqlite");
using (var db = new SQLite.SQLiteConnection(DBPath))
{
    // Create the tables if they don't exist
    db.CreateTable<Customer>();
    db.CreateTable<Item>();
}

The problem:

  1. The code below here will not reset the Primary Key to 0 (or starting number ) for each of the table Once each of them has been used before. Is this code only empty the table but not reset the Primary Key?

How to delete the customers.sqlite ( Database created ) so that this will reset all Primary Key to starting number like 0.

using (var db = new SQLite.SQLiteConnection(DBPath))
{
    db.DeleteAll<Customer>();
    db.DeleteAll<Item>();
}

Upvotes: 1

Views: 1858

Answers (3)

RMarts
RMarts

Reputation: 11

This code can reset Primary key, but if you need delete db, just delete sqlite-db file.

using (var db = new SQLite.SQLiteConnection(localBaseConnection))
                {
                    db.DropTable<YourTableName>();
                    db.CreateTable<YourTableName>();
                    db.Dispose();
                    db.Close();
                }

Upvotes: 1

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

With this queries you can clear the table & then reset auto increment column.

using (var db = new SQLiteConnection(ApplicationData.Current.LocalFolder.Path + "\\db.sqlite"))
{
    db.Query<your_table_name>("delete from your_table_name", "");
    db.Query<sqlite_sequence>("delete from sqlite_sequence where name = ?", "your_table_name");
}

You also have to add this class.

public class sqlite_sequence
{
    public string name { get; set; }
    public int seq { get; set; }
}

Upvotes: 0

CL.
CL.

Reputation: 180240

To ensure that autoincrement valures are reset, you can either delete the database file, or drop the tables.

Upvotes: 0

Related Questions