SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Database creation
Database creation
SQLiteDatabase myDB = this.openOrCreateDatabase("GHRTUTORIALS", MODE_PRIVATE, null);
Queries
Cursor c= myDB.rawQuery("SQL query here", null);
Here is an example demonstrating the use of SQLite Datbase to login
Create your android project with default activity as Login
Save the following code in Login.java
package com.ghr.ghrlogin;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends DBHelper
{
private EditText userName;
private EditText passWord;
private Button login_button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
createTable();
userName=(EditText)findViewById(R.id.editText_Login_UserName);
passWord=(EditText)findViewById(R.id.editText_Login_Password);
login_button=(Button)findViewById(R.id.button_LoginStart);
login_button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Sign_In(arg0);
}
});
}
protected void Sign_In(View v)
{
Cursor c;
String k="",name=userName.getText().toString(),pass=passWord.getText().toString();
if (k.equals(userName.getText().toString()) || k.equals(passWord.getText().toString()) )
{
Toast.makeText(this, "Please enter user details", Toast.LENGTH_SHORT).show();
return;
}
else
{
c = getuserdetails(name,pass);
if(c.getCount()>0)
{
c.moveToFirst();
String userpass=c.getString(c.getColumnIndex("USER_PASSWORD")).toString();
if(userpass.equals(pass))
{
Toast.makeText(this, "Login successfull", Toast.LENGTH_SHORT).show();
this.finish();
Intent i=new Intent(this,MainActivity.class);
startActivity(i);
}
}
else
{
Toast.makeText(this, "Please enter correct user details", Toast.LENGTH_SHORT).show();
return;
}
}
}
}
Save the following code as activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Login" >
<EditText
android:id="@+id/editText_Login_UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/LoginUserName"
android:inputType="textPersonName">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText_Login_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText_Login_UserName"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="@string/LoginPassword"
android:inputType="textPassword" />
<Button
android:id="@+id/button_LoginStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText_Login_Password"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText_Login_Password"
android:layout_marginTop="27dp"
android:text="@string/Login" />
</RelativeLayout>
Save the following code as DBHelper.java
package com.ghr.ghrlogin;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBHelper extends Activity
{
String query;
private SQLiteDatabase myDB= null;
public void createTable()
{
String msg="USERS table created";
query="CREATE TABLE IF NOT EXISTS USERS(USER_NAME VARCHAR(15) PRIMARY KEY,USER_PASSWORD VARCHAR(15) NOT NULL);";
DBOperation(query,msg);
}
public void DBOperation(String values,String msg)
{
myDB=null;
try
{
myDB = this.openOrCreateDatabase("GHRTUTORIALS", MODE_PRIVATE, null);
if(!values.equals("null"))
{
myDB.execSQL(values);
//if(!msg.equals(""))
//Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
else return;
}
catch(Exception e)
{
if (myDB != null)
myDB.close();
}
finally
{
}
}
public Cursor getuserdetails(String name,String pass)
{
Cursor userdetails = null;
myDB= null;
try
{
myDB = this.openOrCreateDatabase("GHRTUTORIALS", MODE_PRIVATE, null);
userdetails= myDB.rawQuery("SELECT * FROM USERS WHERE USER_NAME=\""+name+"\"", null);
if(userdetails.getCount()==0)
{
String values1="INSERT INTO USERS(USER_NAME,USER_PASSWORD)VALUES(\"admin\",\"123\");";
String msg="New user added";
DBOperation(values1,msg);
}
}
catch(Exception e)
{
}
return userdetails;
}
}
Save the following code in strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GHR Login</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello welcome to GHR Tutorials</string>
<string name="title_activity_main">MainActivity</string>
<string name="LoginUserName">"Enter user name "</string>
<string name="LoginPassword">Enter password</string>
<string name="Login">Login</string>
</resources>
Click here to download source code
Output