如何检查表中是否存在db sqlite xamarin iOS
问题描述:
如何检查在哪个表中创建了db数据库与否。如何检查表中是否存在db sqlite xamarin iOS
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
@thanks提前。
答
为什么你需要COUNT(),当然,即使存在,其值必须是1, 我的建议是
SELECT name FROM sqlite_master WHERE type='table' AND name='your table name';
表的方式低吨;)
答
var info = conn.GetTableInfo(tableName);
if (!info.Any())
{
conn.CreateTable<T>();
}
答
扩大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
}
大多数其他答案会返回一个行sqlite异常 – peterincumbria 2017-03-12 14:34:01