How to check a table in db sqlite xamarin iOS

How to check where table is created in db database or not.

var folder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
        SQLiteConnection db = new SQLiteConnection (System.IO.Path.Combine (folder,"note.db"));
        try{
            var existTable = db.Query<TransationTable>("SELECT count(*) FROM sqlite_master WHERE type = 'Table' AND name = 'TransationTable' ");
            Console.WriteLine ("Count {0}",existTable.Count);
          if(existTable.Count == 0){
          tableview.Hidden = true;
          lbl_NotFound.Hidden = false;
        }
    else{
          tableview.Hidden = false;
          lbl_NotFound.Hidden = true;
    }

        }
        catch{
            Console.WriteLine ("Calling Excpetion!");
        }
 }

      

It always gives me a score of 1.
@thanks in advance.

+3


source to share


3 answers


    var info = conn.GetTableInfo(tableName);
    if (!info.Any())
    {
        conn.CreateTable<T>();
    }

      



+11


source


why do you need count (), of course even if it exists the value should be 1, my suggestion is

SELECT name FROM sqlite_master WHERE type='table' AND name='your table name'; 

      



low t table, by the way;)

+3


source


To expand on the meaning of the Jasons point. Better more general way:

string tableName = typeof(Customer).Name;
var customAttributes = typeof(Customer).GetCustomAttributes(typeof(SQLite.Net.Attributes.TableAttribute),false);
if (customAttributes.Count() > 0)
{
    tableName = (customAttributes.First() as SQLite.Net.Attributes.TableAttribute).Name;
}
var info = database.Connection.GetTableInfo(tableName);
if (!info.Any())
{
   //do stuff
}

      

+3


source







All Articles