How To Insert Data In SQLite Data Base In Xamarin Android Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to insert data in SQLite Data Base in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio, or click (Ctrl+Shift+N).

Xamarin

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Xamarin

Step 3

Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page.

Xamarin

Step 4

After opening, the main.axml file will open the main page designer. In this page,  choose which type you want so you can design this page.

Xamarin

Next, delete the default "hello world" button from the source panel by deleting  the button coding. Next, delete the C# button action code from MainActivity.cs page.

Step 5

In this step, add SQLite-net NuGet Package from Nuget Package Manager.

Go to Solution Explorer-->Your Project-->Right Click-->Manage Nuget Packages.

Xamarin

Step 6

Now Browse to search the SQLite Nuget Package. You need to select the SQLite-net and click Install.

Xamarin

Step 7

Now click ok to Install.

Xamarin

Step 8

In this step, design your app. Now, go to the toolbox window.and scroll down. You will see all the tools and controls.

You need to drag and drop the Button.

Xamarin

Step 9

Now, go to the Properties window. You need to edit the Button Id Value and Text Value(EX:android:id="@+id/btninsert" android:text="INSERT").

Xamarin

Step 10

In this step, go to the Main.axml page Source Panel. Note the Button Id Value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <Button android:text="INSERT" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btninsert" /> </LinearLayout>  
Xamarin

Step 11

Add one layout called Insert.axml.

Go to Solution Explorer-->Resource-->Layout-->Right click-->Add-->New Item(Ctrl+Shift+A).

Xamarin

Step 12

Now, select Android Layout and give name (Insert.axml).

Xamarin

Step 13

Now, go to the toolbox window and drag and drop two Plain Text (EditText).

Xamarin

Step 14

You need to drag and drop the Button.

Xamarin

Step 15

Now, go to the Properties window. You need to edit the PlainText(EditText) Id Value(EX:android:id="@+id/txtname").

Xamarin

Step 16

You need to edit the PlainText(EditText) Id Value(EX:android:id="@+id/txtemail").

Xamarin

Step 17

And also, edit the Button Id Value and Text Value (EX:android:id="@+id/btnsave" android:text="STORE").

Xamarin

Step 18

In this step, go to the Main.axml page Source Panel. Note the Button Id Value and EditText Id Values.

Insert.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <TextView android:text="Store Data" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" android:gravity="center" />  
  3.     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtname" android:hint="NAME" />  
  4.     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtemail" android:hint="someone@example.com" />  
  5.     <Button android:text="STORE" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnsave" /> </LinearLayout>  
Xamarin

Step 19

In this step, create a Database Table called Persontable.cs.

Go to Solution Explorer-->Your app-->Right click-->Add-->New Item(Ctrl+Shift+A).

Xamarin

Step 20

Now, select the Class and give name Persontable.cs. and click add.

Xamarin

Step 21

Now, go to the Persontable.cs and write the following code to create the Persontable.and add SQLite Namespace.

Persontable.cs
  1. using SQLite;  
  2. namespace XamarinSQLite {  
  3.     class Persontable {  
  4.         [PrimaryKey, AutoIncrement, Column("_Id")]  
  5.         public int id {  
  6.             get;  
  7.             set;  
  8.         } // AutoIncrement and set primarykey  
  9.         public string Name {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         [MaxLength(30)]  
  14.         public string Email {  
  15.             get;  
  16.             set;  
  17.         }  
  18.     }  
  19. }  
Xamarin

Step 22

In this step Create one Activity its name is called InsertActivity.cs.

Go to Solution Explorer-->Your app-->Right click-->Add-->New Item(Ctrl+Shift+A).

Xamarin

Step 23

Now, select the Activity and give name InsertActivity.cs. and click add.

Xamarin

Step 24

After this, add these two Namespaces.

InsertActivity.cs
  1. using System.IO;  
  2. using SQLite;  
Xamarin

Step 25

Now, write the following code from OnCreate() Method, and add three variables in InsertActivity.cs page.

InsertActivity.cs
  1. //Variables  
  2. Button btncreate;  
  3. EditText txtname;  
  4. EditText txtemail;  
  5. protected override void OnCreate(Bundle savedInstanceState) {  
  6.     base.OnCreate(savedInstanceState);  
  7.     SetContentView(Resource.Layout.Insert);  
  8.     txtname = FindViewById < EditText > (Resource.Id.txtname);  
  9.     txtemail = FindViewById < EditText > (Resource.Id.txtemail);  
  10.     btncreate = FindViewById < Button > (Resource.Id.btnsave);  
  11.     btncreate.Click += Btncreate_Click;  
  12. }  
Xamarin

Step 26

In this step create Btncreate_Click method and write the following code.

InsertActivity.cs
  1. private void Btncreate_Click(object sender, EventArgs e) {  
  2.     try {  
  3.         string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Person.db3");  
  4.         var db = new SQLiteConnection(dpPath);  
  5.         db.CreateTable < Persontable > ();  
  6.         Persontable tbl = new Persontable();  
  7.         tbl.Name = txtname.Text;  
  8.         tbl.Email = txtemail.Text;  
  9.         db.Insert(tbl);  
  10.         clear();  
  11.         Toast.MakeText(this"Data Store Successfully...,", ToastLength.Short).Show();  
  12.     } catch (Exception ex) {  
  13.         Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();  
  14.     }  
  15. }  
  16. void clear() {  
  17.     txtname.Text = "";  
  18.     txtemail.Text = "";  
  19. }  
Xamarin

Step 27

In this step Go to the MainActivity.cs page and add two namespaces and add one Variable.

MainActivity.cs

  1. using System.IO;  
  2. using SQLite;  
  3. Button btninsert; //Variable  
Xamarin

Step 28

Now, Write the following code from MainActivity.cs page.

MainActivity.cs
  1. using System.IO;  
  2. using SQLite;  
  3. namespace XamarinSQLite {  
  4.     [Activity(Label = "XamarinSQLite", MainLauncher = true, Icon = "@drawable/icon")]  
  5.     public class MainActivity: Activity {  
  6.         Button btninsert;  
  7.         protected override void OnCreate(Bundle bundle) {  
  8.             base.OnCreate(bundle);  
  9.             // Set our view from the "main" layout resource  
  10.             SetContentView(Resource.Layout.Main);  
  11.             btninsert = FindViewById < Button > (Resource.Id.btninsert);  
  12.             btninsert.Click += delegate {  
  13.                 StartActivity(typeof(InsertActivity));  
  14.             };  
  15.             CreateDB();  
  16.         }  
  17.         public string CreateDB() {  
  18.             var output = "";  
  19.             output += "Creating Databse if it doesnt exists";  
  20.             string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Person.db3"); //Create New Database  
  21.             var db = new SQLiteConnection(dpPath);  
  22.             output += "\n Database Created....";  
  23.             return output;  
  24.         }  
  25.     }  
  26. }  
  27. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  28. if ('this_is' == /an_example/) {  
  29.     of_beautifier();  
  30. else {  
  31.     var a = b ? (c % d) : e[f];  
  32. }  
Xamarin

Step 29

If you have an Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone.

You will see your app is running successfully.

Now click Insert Button.

Xamarin

Now, you can give the input.and click STORE button.

Xamarin

Xamarin

Now, you can see that your Data has been stored successfully.

Xamarin

Summary

So, this was the process of how to insert data in SQLite Database in Xamarin Android app, using Visual Studio 2015.