Skip to main content

How to use SQLite libarary in Windows 8 Apps


Hi Guys ...

SQLite is a software library that implements a self-containedserverlesszero-configuration,transactional SQL database engine.

Now SQLite’s branch for WinRT is available and we can start to use it to store the data of our applications, with the help of another library called sqlite-net, that provides LINQ based APIs to work with data, both with sync and async support.



Installing the SQLite for Windows Runtime package

  • First thing is we should do is install the "SQLite for Windows Runtime package.
     From the Tools menu, choose 
    Extensions and Updates and then choose the Online section (on the left of the dialog) and search for ‘sqlite’ in the search term.  This will show you the SQLite for Windows Runtime package: Click Install




















  • Then add following two libraries to the Reference. Right click on reference and select Add Reference ..
    SQLite for Windows Runtime
    Microsoft Visual C++ Runtime Package







  •   
Create Database 
Now i am going to create a database name "School".  This school DB contain set of tables and we  have to create classes for each tables.
I am going to create Table name Student and it has three attributes(columns) .
  • Id
  • Name
  • Course 
This is the class for Student Table ...

 public class Student
 {  
   [PrimaryKey, AutoIncrement]  
   public int Id { get; set; }  
   [MaxLength(30)]  
   public string Name { get; set; }  
   [MaxLength(30)]  
   public string Course { get; set; }  
 }  

Now you can create the Database 

 private async void CreateDatabase()  
 {  
   SQLiteAsyncConnection conn = new SQLiteAsyncConnection("School");  
   await conn.CreateTableAsync<Student>();  
 }  

The first step is to create a SQLiteAsynConnection object, that identifies the connection to the database, like in the example: the parameter passed to the constructor is the name of the file that will be created in the local storage. 

Adding And Reading Data

  • To insert data we use the InsertAsync method
 private async void Insert_Click_1(object sender, RoutedEventArgs e)  
 {  
   SQLiteAsyncConnection conn = new SQLiteAsyncConnection("School");  
   Student Student= new Student  
   {  
     Name = "Sanka",  
     Course = "BTech"  
   };  
   await conn.InsertAsync(Student);  
 }  


  • We can access directly to the table using the Table<T>object: it supports LINQ queries, so we can simply use LINQ to search for the data we need. Then, we can call the ToListAsync method to get a List<T> of objects that matches the specified query
 private async void Read_Click_1(object sender, RoutedEventArgs e)  
 {  
   SQLiteAsyncConnection conn = new SQLiteAsyncConnection("School");  
   var query = conn.Table<Student>().Where(x => x.Name == "Sanka");  
   var result = await query.ToListAsync();  
   foreach (var item in result)  
   {  
     Debug.WriteLine(string.Format("{0}: {1} {2}", item.Id, item.Name, item.Course));  
   }  
 }  


Thank You guys !!!!!!!!!!!!!!
I think this post will help you .......................

Comments

Popular posts from this blog

Multithreading C# : Part 01

Multithreading with C# Think about old days, you have one central processing unit (CPU) in your pc which is capable of executing one operation at a time. If we have 5 operations to run then we have to wait till one operation is completed and then only other one can start. What will happen if the running operation has a bug and got stuck, then whole computer going to be freeze and useless unless we restart it. This is a huge problem. So if we can run multiple operation in the same time that would be great because it will solve this problem.

Windows 8 Icons: Segoe UI Symbol

Whatz up guyz... In this post i am going to tell you about "Windows 8 Icons" . If you ever used a windows 8 app, you know that there are so many standard Icons. Actually we do not want to add images for each button, simply we can use the character map for obtain the icons.Go to the character map and select the font as "Segoe UI Symbol" .Then you will see lot of standard symbols in the character map.

C# Character Escape Sequences

Character combinations consisting of a backslash ( \ ) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.