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

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup. 

Visual Studio 2013 New Editor Features

In Visual Studio 2013, we have introduced new features that boost productivity and save time when working inside the Editor. Some of these are new features and some are the most popular extensions from  Productivity Power Tools . These features are a result of the feedback you gave us through  User Voice  requests, forum posts and Connect bugs. The MVP community also helped us pick some of these experiences. Our primary focus for the Editor in this version is to keep the developer in context as much as possible. This blog post describes capabilities that bring information to your fingertips and allow you to do as much as possible without leaving your place in code.