· 8 years ago · May 09, 2018, 09:42 AM
1var folder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
2 SQLiteConnection db = new SQLiteConnection (System.IO.Path.Combine (folder,"note.db"));
3 try{
4 var existTable = db.Query<TransationTable>("SELECT count(*) FROM sqlite_master WHERE type = 'Table' AND name = 'TransationTable' ");
5 Console.WriteLine ("Count {0}",existTable.Count);
6 if(existTable.Count == 0){
7 tableview.Hidden = true;
8 lbl_NotFound.Hidden = false;
9 }
10 else{
11 tableview.Hidden = false;
12 lbl_NotFound.Hidden = true;
13 }
14
15 }
16 catch{
17 Console.WriteLine ("Calling Excpetion!");
18 }
19 }
20
21var info = conn.GetTableInfo(tableName);
22 if (!info.Any())
23 {
24 conn.CreateTable<T>();
25 }
26
27SELECT name FROM sqlite_master WHERE type='table' AND name='your table name';
28
29string tableName = typeof(Customer).Name;
30var customAttributes = typeof(Customer).GetCustomAttributes(typeof(SQLite.Net.Attributes.TableAttribute),false);
31if (customAttributes.Count() > 0)
32{
33 tableName = (customAttributes.First() as SQLite.Net.Attributes.TableAttribute).Name;
34}
35var info = database.Connection.GetTableInfo(tableName);
36if (!info.Any())
37{
38 //do stuff
39}
40
41public MainPage()
42 {
43 InitializeComponent();
44
45 conn = DependencyService.Get<ISQLite>().GetConnection();
46
47
48 try
49 {
50 var existTable = conn.Query<Student>("SELECT name FROM sqlite_master WHERE type='table' AND name='Student'; ");
51
52 if (!(existTable.Count > 0))
53 {
54 //if table not exists create table
55 conn.CreateTable<Student>();//Student is Class .cs represents table and columns
56 /*
57 public class Student
58 { [PrimaryKey ,AutoIncrement]
59 public int ID { get; set; }
60 public String Name { get; set; }
61 public String Address { get; set; }
62 }
63 */
64 }//else ignore
65 }
66 catch (Exception Ex)
67 {
68 DisplayAlert("Alert", "SQL Exception while starting Application", "Ok");
69 }
70
71
72
73 }