[英]how to check table exist in db sqlite xamarin iOS
如何檢查在db數據庫中創建表的位置。
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!");
}
}
它總是給我數1。
@提前致謝。
var info = conn.GetTableInfo(tableName);
if (!info.Any())
{
conn.CreateTable<T>();
}
為什么你需要count(),當然即使它存在,值必須為1,我的建議是
SELECT name FROM sqlite_master WHERE type='table' AND name='your table name';
順便提一下桌子;)
擴大Jasons點。 更通用的方法是:
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
}
public MainPage()
{
InitializeComponent();
conn = DependencyService.Get<ISQLite>().GetConnection();
try
{
//Student is table name ,replace student with your table name
var existTable = conn.Query<Student>("SELECT name FROM sqlite_master WHERE type='table' AND name='Student'; ");
if ((existTable.Count > 0))
{
//Write code if table exists
}
}
catch (Exception Ex)
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.