Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQLite with UWP

2,421 views

Published on

Local Data Base SQLite for Windows 10 Universal Windows Platform App Development.

Published in: Technology
  • Be the first to comment

SQLite with UWP

  1. 1. Prepared by ENG SOON CHEAH Microsoft MVP
  2. 2. Install SQLite-UAP extensions form NuGet Package Manager
  3. 3. Next Install SQLite.Net-PCL extension from NuGet Package
  4. 4. • Include 4 functions CRUD Operations • Bind data to a ListBox
  5. 5. <Grid Background="#FFF589E2"> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Button x:Name="CreateDBbutton" Grid.Row="0" Content="Create Local Database" HorizontalAlignment="Center" VerticalAlignment="Top" Click="button_Click" /> <Button x:Name="create" Grid.Row="1" Content="Create New Students" HorizontalAlignment="Center" Click="creat e_Click"></Button> <Button x:Name="read" Grid.Row="2" Content="Read Students List" Width="300" Click="read_Click" HorizontalAlig nment="Center"></Button> <Button x:Name="update" Grid.Row="3" Content="Update Details" Width="300" Click="update_Click" HorizontalAli gnment="Stretch"></Button> <ListView x:Name="allstudents" HorizontalAlignment="Stretch" Grid.Row="4"> <ListView.ItemTemplate> <DataTemplate> <TextBlock x:Name="ee" Text="{Binding Name}" FontSize="14"></TextBlock> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid>
  6. 6. public class Students { [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement] public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string Mobile { get; set; } public Students() {} public Students(string name, string address, string mobile) { Name = name; Address = address; Mobile = mobile; } }
  7. 7. public static void CreateDatabase() { var sqlpath = System.IO.Path.Combine(Windows.Stora ge.ApplicationData.Current.LocalFolder.Path, "Studentdb. sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLit e.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT. SQLitePlatformWinRT(), sqlpath)) { conn.CreateTable < Students > (); } }
  8. 8. public void Insert(Students objContact) { var sqlpath = System.IO.Path.Combine(Windows.Storage.Appli cationData.Current.LocalFolder.Path, "Studentdb.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.S QLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatform WinRT(), sqlpath)) { conn.RunInTransaction(() => { conn.Insert(objContact); }); } }
  9. 9. // Retrieve the specific contact from the database. public Students ReadContact(int contactid) { var sqlpath = System.IO.Path.Combine(Windows.Storage.Appli cationData.Current.LocalFolder.Path, "Studentdb.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.S QLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatform WinRT(), sqlpath)) { var existingconact = conn.Query < Students > ("select * fro m Students where Id =" + contactid).FirstOrDefault(); return existingconact; } }
  10. 10. //Read All Student details public ObservableCollection < Students > ReadAllStudents() { var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Cur rent.LocalFolder.Path, "Studentdb.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnectio n(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath)) { List < Students > myCollection = conn.Table < Students > ().ToList < Stud ents > (); ObservableCollection < Students > StudentsList = new ObservableCollecti on < Students > (myCollection); return StudentsList; } }
  11. 11. //Update student detaisl public void UpdateDetails(string name) { var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Student db.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.Win RT.SQLitePlatformWinRT(), sqlpath)) { var existingconact = conn.Query < Students > ("select * from Students where Name =" + name).FirstOrDe fault(); if (existingconact != null) { existingconact.Name = name; existingconact.Address = "NewAddress"; existingconact.Mobile = "962623233"; conn.RunInTransaction(() => { conn.Update(existingconact); }); } } }
  12. 12. //Delete all student or delete student table public void DeleteAllContact() { var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformW inRT(), sqlpath)) { conn.DropTable < Students > (); conn.CreateTable < Students > (); conn.Dispose(); conn.Close(); } } Delete specific student //Delete specific student public void DeleteContact(int Id) { var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Studentdb.sqlite"); using(SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformW inRT(), sqlpath)) { var existingconact = conn.Query < Students > ("select * from Studentdb where Id =" + Id).FirstOrDefault(); if (existingconact != null) { conn.RunInTransaction(() => { conn.Delete(existingconact); }); } } }
  13. 13. • Local Data Base SQLite for Windows 10 https://code.msdn.microsoft.com/Local- Data-Base-SQLite-for-5e6146aa

×