如何检查表中存在的数据库sqlite的Xamarin iOS
如何检查在db数据库中是否创build表。
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,我的build议是
SELECT name FROM sqlite_master WHERE type='table' AND name='your table name';
与低t的表;)
扩大贾森点。 更通用的方法是:
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 }