In this article, we will learn about the basics of SQLite database for all platforms (Android, iOS, and Windows).

Step 1:

Before going through this articles. I would like to request to see my following previous articles:

Step 2:

First create a Xamarin.Forms project with Portable Class Library (PCL) as discussed in my previous article: Xamarin.Forms: Create Your First Xamarin App For iOS, Android, And Windows

Step 3:

Now we will install sqlite-net-pcl from NuGet Package Manager.

Under PCL project, right click on References and from the NuGet Package Manager, search sqlite-net-pcl and install that within Portable Class Library (PCL) project.

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\2.png

Step 4:

Install the same SQLite library for each platform (Android, iOS, and Windows) under each project. So let’s add sqlite-net-pcl for Android first.

Right click on References under Android project and install sqlite-net-pcl in a similar way like above.

Step 5:

Add an XAML Page with name FormsPage.xaml. For this right click on PCL project and add Forms XAML Page.

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\5.JPG

Step 6:

In the FormsPage.xaml, we add two labels, two input fields, and two buttons as:

 

 

It will create a UI like this:

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\Screenshot_2016-07-03-12-43-01.png

Step 7:

We need to create an interface class to define platform specific database file save and creating database connection.

So in you PCL project add an Interface class with name ISQLite.

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\2.JPG

Complete code for ISQLite.cs:

 

Step 8:

Next, we create a Model class with name “Member”.

Right Click on PCL Project > Add > Class > Name it Member and Click Add.

Here, Name,  Age is getter setter used to set and get the values with ID which has PrimaryKey and AutoIncrement properties derived from SQLite.

Complete Code snippet for Member class is:

 

Step 9:

Now we add another class that represents database which includes all the logic for database operations like Create, Read, Write, Delete, Update, etc.

Let’s add another class with name MemberDatabase for DB logics.

 

Step 10:

Now we need to add platform specific database file creation code to save database file and to create database connection since each platform has different folder environment.

FOR ANDROID:

Add a class with name Andorid_SQLite and implement ISQLite interface.

Under Android Project > Right Click > Add > New Class.

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\4.JPG

In Andorid_SQLite class, update your code with the following code snippet:

 

FOR WINDOWS:

Add a similar class like above inside Windows Phone/Windows Project. Name it Windows_SQLite.

In Windows_SQLite class, update your code with the following code snippet:

 

FOR IOS:

Add a similar class like above inside iOS Project. Name it IOS_SQLite.

In IOS_SQLite class, update your code with the following code snippet:

 

Step 11:

In the code behind of FormsPage.xaml.cs, we will initialize MemberDatabase and Member class and call the AddMember function.

Click events InsertMember and ShowMembers are defined here. And we navigate to MemberList page inside the ShowMember function.

Complete code snippet for FormsPage.xaml.cs:

 

Step 12:

In the App.cs, update App constructor with:

 

Step 13:

Let’s add another XAML page that shows the list of members from the database.

Inside PCL, Add a Forms XAML Page with name MembersList.xaml

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\6.JPG

In the MemberList.xaml, add a ListView inside ContentPage.Content. Inside the ListView, we have ItemTemplate and DataTemplate. We use ViewCell to display our content.

Complete XAML code will be:

 

Step 14:

In the code behind MemberList.xaml.cs, update your code with this:

 

Step 15:

Run the application in your android or windows devices/emulators. When you insert members and then view them, then you will be navigated to the Member List Page and see the following output.

Android Screenshots:

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\Screenshot_2016-07-03-16-00-42.png C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\Screenshot_2016-07-03-16-01-13.png

Windows Screenshots

C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\wp_ss_20160703_0002.png C:\Users\Kishor Bikram Oli\Desktop\Blogs\20160702\wp_ss_20160703_0001.png

 

Note: Since whole project has very high size. So download the Portable Class Library (PCL) only from GitHub https://github.com/olikishor/XamarinForms_SQLiteLibrary

That’s it.

Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">