What is SQLite?
SQLite database has methods to create, delete, and execute SQL commands, and perform other common database management tasks. More details.
Let’s start,
Step 1
Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App.
Select Blank App. Give the project name and the project location.
Step 2
Next, go to Solution Explorer-> Project Name-> Resouces->layout. Right click on Add. A new dialog box will open. Select Android layout, give name for Newuser.axml and add the New Layout Page.
Step 3
Go to Solution Explorer-> Project Name. Right click on Add. Open new dialog box. Select Activity and give name for RegisterActivity.cs. Now, add the New Activity Class.
Step 4
Next, we need to create the database table, so we create one Data Layer class. Go to Solution Explorer-> Project Name. Right click to Add. Open new dialog box. Select Class, give name for LoginTable.cs and after it, add New Class.
Step 5
Go to Solution Explorer-> Project Name-> References. Right Click to manage Nuget Packages and open new dialog box. This dialog box searches SQLite. Install SQLite-Net Packages.
Step 6
Next, Open the Solution Explorer-> Project Name->Resources->Layout->Main.axml. Click Open Design View. Here, create large text, two plain text and two buttons.
Step 7
Next step is to Open Solution Explorer-> Project Name->Resources->Layout->Newuser.axml, click Open Design View. Here, create large text, two plain text and one button.
Step 8
Next is to open Solution Explorer-> Project Name->LoginTable.cs, click Open CS code Page View and create table columns.
C# Code
- [PrimaryKey, AutoIncrement, Column("_Id")]
-
- public int id { get; set; }
-
- [MaxLength(25)]
-
- public string username { get; set; }
-
- [MaxLength(15)]
-
- public string password { get; set; }
Step 9
Now, open Solution Explorer-> Project Name->MainActivity.cs, click Open CS code Page view and add the following namespaces. First, we create database. So, after OnCreate(), create new method with name of CreateDB().
C# Code
- public class MainActivity: Activity
- {
- EditText txtusername;
- EditText txtPassword;
- Button btncreate;
- Button btnsign;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
-
-
- btnsign = FindViewById < Button > (Resource.Id.btnlogin);
- btncreate = FindViewById < Button > (Resource.Id.btnregister);
- txtusername = FindViewById < EditText > (Resource.Id.txtusername);
- txtPassword = FindViewById < EditText > (Resource.Id.txtpwd);
- btnsign.Click += Btnsign_Click;
- btncreate.Click += Btncreate_Click;
- CreateDB();
- }
- private void Btncreate_Click(object sender, EventArgs e)
- {
- StartActivity(typeof(RegisterActivity));
- }
- private void Btnsign_Click(object sender, EventArgs e)
- {
- try
- {
- string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
- var db = new SQLiteConnection(dpPath);
- var data = db.Table < LoginTable > ();
- var data1 = data.Where(x => x.username == txtusername.Text && x.password == txtPassword.Text).FirstOrDefault();
- if (data1 != null)
- {
- Toast.MakeText(this, "Login Success", ToastLength.Short).Show();
- }
- else
- {
- Toast.MakeText(this, "Username or Password invalid", ToastLength.Short).Show();
- }
- }
- catch (Exception ex)
- {
- Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
- }
- }
- public string CreateDB()
- {
- var output = "";
- output += "Creating Databse if it doesnt exists";
- string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
- var db = new SQLiteConnection(dpPath);
- output += "\n Database Created....";
- return output;
- }
- }
Here, the database name is “user.db3”.
Step 10
Next, open Solution Explorer-> Project Name-> RegisterActivity.cs. Click Open CS code Page view, then add the below namespaces. Next, write the following code.
C# Code
- public class RegisterActivity: Activity
- {
- EditText txtusername;
- EditText txtPassword;
- Button btncreate;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- SetContentView(Resource.Layout.Newuser);
-
- btncreate = FindViewById < Button > (Resource.Id.btn_reg_create);
- txtusername = FindViewById < EditText > (Resource.Id.txt_reg_username);
- txtPassword = FindViewById < EditText > (Resource.Id.txt_reg_password);
- btncreate.Click += Btncreate_Click;
- }
- private void Btncreate_Click(object sender, EventArgs e)
- {
- try
- {
- string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
- var db = new SQLiteConnection(dpPath);
- db.CreateTable < LoginTable > ();
- LoginTable tbl = new LoginTable();
- tbl.username = txtusername.Text;
- tbl.password = txtPassword.Text;
- db.Insert(tbl);
- Toast.MakeText(this, "Record Added Successfully...,", ToastLength.Short).Show();
- } catch (Exception ex) {
- Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
- }
- }
- }
Step 11
Press F5 or build and run the Application. First image is to register the new user and then, the next image is to login the user.
Finally, we successfully created login using SQLite database.