I am unable to drop tables completely from sqLite database in XAMARIN using command db.DropTable<tablename>. It gets dropped at first but appears again every time I restart the application. Also I have multiple tables with the same name and I need to drop them all. What should I do?
I am developing an android application in Visual Studio using XAMARIN and C#.
Unfortunately I created many tables with the same name. Now I need to drop them.
I am using following code to create tables and database:-
namespace Test
{
[Table("OrderDetails")]
public class OrderDetails
{
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
[MaxLength(50)]
public string payeename { get; set; }
[MaxLength(100)]
public string commodity { get; set; }
public int give { get; set; }
public int take { get; set; }
public DateTime date { get; set; }
}
[Activity(Label = "Test", MainLauncher = true)]
public class MainActivity : Activity
{
[Table("Items")]
public class PayeeMaster
{
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
[MaxLength(8)]
public string FirstName { get; set; }
[MaxLength(8)]
public string LastName { get; set; }
}
SQLiteConnection db;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
CreateDatabase();
}
public bool ifExists_Table(string tablename)
{
SQLiteCommand command = db.CreateCommand("SELECT COUNT(1) FROM SQLITE_MASTER WHERE TYPE = @TYPE AND NAME = @NAME");
command.Bind("@TYPE", "table");
command.Bind("@NAME", tablename);
int result = command.ExecuteQuery<int>();
return (result > 0);
}
public void CreateDatabase() {
try
{
string dbPath = Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
"Master.db3");
db = new SQLiteConnection(dbPath);
}
catch (IOException ex)
{
var reason = string.Format("The database failed to create -
reason {0}", ex.Message);
}
if (!ifExists_Table("PayeeMaster"))
{
db.CreateTable<PayeeMaster>();
}
else
{
var count = db.Table<PayeeMaster>().Count();
}
if (!ifExists_Table("OrderDetails"))
{
db.CreateTable<OrderDetails>();
}
else {
var count = db.Table<OrderDetails>().Count();
}
}
}
if (db.Table<PayeeMaster>().Count() ==0) db.CreateTable<PayeeMaster>();⬅️⬅️⬅️