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).
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.
Step 3
Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page.
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.
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.
Step 6
Now Browse to search the SQLite Nuget Package. You need to select the SQLite-net and click Install.
Step 7
Now click ok to Install.
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.
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").
Step 10
In this step, go to the Main.axml page Source Panel. Note the Button Id Value.
Main.axml
- <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">
- <Button android:text="INSERT" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btninsert" /> </LinearLayout>
Step 11
Add one layout called Insert.axml.
Go to Solution Explorer-->Resource-->Layout-->Right click-->Add-->New Item(Ctrl+Shift+A).
Step 12
Now, select Android Layout and give name (Insert.axml).
Step 13
Now, go to the toolbox window and drag and drop two Plain Text (EditText).
Step 14
You need to drag and drop the Button.
Step 15
Now, go to the Properties window. You need to edit the PlainText(EditText) Id Value(EX:android:id="@+id/txtname").
Step 16
You need to edit the PlainText(EditText) Id Value(EX:android:id="@+id/txtemail").
Step 17
And also, edit the Button Id Value and Text Value (EX:android:id="@+id/btnsave" android:text="STORE").
Step 18
In this step, go to the Main.axml page Source Panel. Note the Button Id Value and EditText Id Values.
Insert.axml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
- <TextView android:text="Store Data" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" android:gravity="center" />
- <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtname" android:hint="NAME" />
- <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtemail" android:hint="[email protected]" />
- <Button android:text="STORE" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnsave" /> </LinearLayout>
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).
Step 20
Now, select the Class and give name Persontable.cs. and click add.
Step 21
Now, go to the Persontable.cs and write the following code to create the Persontable.and add SQLite Namespace.
Persontable.cs - using SQLite;
- namespace XamarinSQLite {
- class Persontable {
- [PrimaryKey, AutoIncrement, Column("_Id")]
- public int id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- [MaxLength(30)]
- public string Email {
- get;
- set;
- }
- }
- }
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).
Step 23
Now, select the Activity and give name InsertActivity.cs. and click add.
Step 24
After this, add these two Namespaces.
InsertActivity.cs
- using System.IO;
- using SQLite;
Step 25
Now, write the following code from OnCreate() Method, and add three variables in InsertActivity.cs page.
InsertActivity.cs
-
- Button btncreate;
- EditText txtname;
- EditText txtemail;
- protected override void OnCreate(Bundle savedInstanceState) {
- base.OnCreate(savedInstanceState);
- SetContentView(Resource.Layout.Insert);
- txtname = FindViewById < EditText > (Resource.Id.txtname);
- txtemail = FindViewById < EditText > (Resource.Id.txtemail);
- btncreate = FindViewById < Button > (Resource.Id.btnsave);
- btncreate.Click += Btncreate_Click;
- }
Step 26
In this step create Btncreate_Click method and write the following code.
InsertActivity.cs - private void Btncreate_Click(object sender, EventArgs e) {
- try {
- string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Person.db3");
- var db = new SQLiteConnection(dpPath);
- db.CreateTable < Persontable > ();
- Persontable tbl = new Persontable();
- tbl.Name = txtname.Text;
- tbl.Email = txtemail.Text;
- db.Insert(tbl);
- clear();
- Toast.MakeText(this, "Data Store Successfully...,", ToastLength.Short).Show();
- } catch (Exception ex) {
- Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
- }
- }
- void clear() {
- txtname.Text = "";
- txtemail.Text = "";
- }
Step 27
In this step Go to the MainActivity.cs page and add two namespaces and add one Variable.
MainActivity.cs
- using System.IO;
- using SQLite;
- Button btninsert;
Step 28
Now, Write the following code from MainActivity.cs page.
MainActivity.cs - using System.IO;
- using SQLite;
- namespace XamarinSQLite {
- [Activity(Label = "XamarinSQLite", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity: Activity {
- Button btninsert;
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- btninsert = FindViewById < Button > (Resource.Id.btninsert);
- btninsert.Click += delegate {
- StartActivity(typeof(InsertActivity));
- };
- CreateDB();
- }
- public string CreateDB() {
- var output = "";
- output += "Creating Databse if it doesnt exists";
- string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Person.db3");
- var db = new SQLiteConnection(dpPath);
- output += "\n Database Created....";
- return output;
- }
- }
- }
-
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) : e[f];
- }
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.
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.
Now, you can give the input.and click STORE button.
Now, you can see that your Data has been stored successfully.
Summary So, this was the process of how to insert data in SQLite Database in Xamarin Android app, using Visual Studio 2015.