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

Load an Addin into DraftSight

These days i am developing two plugins for Autocad 2012 &  DraftSight 2013.so when i was developing the plugin i got in to big trouble.Normally in the Autocad , we can load the .dll file simply typing the command "NETLOAD" but in the Draftsight i could not able to do that. It said  " D:\c \projects\c projects\DSAddinCSharp1\ DSAddinCSharp1\bin\Debug\ DSAddinCSharp1.dll is not a valid add-in "

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.

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.