19 SQLIte SImple Database Example
Algorithm:
Step 1)Create an Android Studio Project named as SQLiteCURD.
Step 2) open activity_main.xml and design UI page.
Step 3)Create a database Helper class named as DB_Controller which extends SQLiteOpenHelper .
It is abstract class so we need to implement the abstract methods
tools:context=".MainActivity">
<TextView
android:id="@+id/textView4"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginTop="47dp"
android:text="UID"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView"
android:layout_width="49dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="114dp"
android:text="NAME"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginTop="181dp"
android:text="PASSWORD"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="233dp"
android:text="EMAIL"
tools:ignore="RtlCompat" />
<EditText
android:id="@+id/uid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_toEndOf="@+id/textView4"
android:layout_toRightOf="@+id/textView4"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_below="@+id/uid"
android:layout_alignBaseline="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="37dp"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="66dp"
android:layout_marginRight="66dp"
android:layout_marginBottom="26dp"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/email"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="27dp"
android:layout_marginEnd="66dp"
android:layout_marginRight="66dp"
android:layout_marginBottom="6dp"
android:layout_toEndOf="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginEnd="68dp"
android:layout_marginRight="68dp"
android:layout_marginBottom="243dp"
android:layout_toEndOf="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginTop="29dp"
android:layout_marginBottom="166dp"
android:text="ADD DATA"
android:onClick="addData"/>
<Button
android:id="@+id/button2"
android:layout_width="86dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:layout_marginBottom="-4dp"
android:layout_toEndOf="@+id/button"
android:layout_toRightOf="@+id/button"
android:text="VIEW ALL"
android:onClick="viewAll"/>
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:layout_marginBottom="68dp"
android:layout_toStartOf="@+id/button4"
android:layout_toLeftOf="@+id/button4"
android:text="UPDATE"
android:onClick="update"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="51dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="65dp"
android:text="DELETE"
android:onClick="delete"/>
</RelativeLayout>
Step 1)Create an Android Studio Project named as SQLiteCURD.
Step 2) open activity_main.xml and design UI page.
Step 3)Create a database Helper class named as DB_Controller which extends SQLiteOpenHelper .
It is abstract class so we need to implement the abstract methods
1)onCreate()
2)onUpgrade() and
3)constructor for helper class as follows
public class DB_Controller extends SQLiteOpenHelper
{
//constructor
public DB_Controller(@androidx.annotation.Nullable Context context, @androidx.annotation.Nullable String name, @androidx.annotation.Nullable SQLiteDatabase.CursorFactory factory, int version)
{super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Step 4) go to MainActivity class and create an object of SQLiteDatabase
Program:
activity_main.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" tools:context=".MainActivity">
<TextView
android:id="@+id/textView4"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginTop="47dp"
android:text="UID"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView"
android:layout_width="49dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="114dp"
android:text="NAME"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginTop="181dp"
android:text="PASSWORD"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="233dp"
android:text="EMAIL"
tools:ignore="RtlCompat" />
<EditText
android:id="@+id/uid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:layout_toEndOf="@+id/textView4"
android:layout_toRightOf="@+id/textView4"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_below="@+id/uid"
android:layout_alignBaseline="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="37dp"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="66dp"
android:layout_marginRight="66dp"
android:layout_marginBottom="26dp"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/email"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="27dp"
android:layout_marginEnd="66dp"
android:layout_marginRight="66dp"
android:layout_marginBottom="6dp"
android:layout_toEndOf="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginEnd="68dp"
android:layout_marginRight="68dp"
android:layout_marginBottom="243dp"
android:layout_toEndOf="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginTop="29dp"
android:layout_marginBottom="166dp"
android:text="ADD DATA"
android:onClick="addData"/>
<Button
android:id="@+id/button2"
android:layout_width="86dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:layout_marginBottom="-4dp"
android:layout_toEndOf="@+id/button"
android:layout_toRightOf="@+id/button"
android:text="VIEW ALL"
android:onClick="viewAll"/>
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:layout_marginBottom="68dp"
android:layout_toStartOf="@+id/button4"
android:layout_toLeftOf="@+id/button4"
android:text="UPDATE"
android:onClick="update"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="51dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="65dp"
android:text="DELETE"
android:onClick="delete"/>
</RelativeLayout>
MainActivity.java:
package com.example.basha.sqlitesimplecurd;
import android.app.AlertDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText uid,name,password,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
uid = (EditText)findViewById(R.id.uid);
name = (EditText)findViewById(R.id.name);
password = (EditText)findViewById(R.id.password);
email = (EditText)findViewById(R.id.email);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void addData(View view) {
boolean isInserted = myDb.insertData(name.getText().toString(),
password.getText().toString(),
email.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
public void viewAll(View view) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("Surname :"+ res.getString(2)+"\n");
buffer.append("Marks :"+ res.getString(3)+"\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
public void update(View view) {
boolean isUpdate = myDb.updateData(uid.getText().toString(),
name.getText().toString(),
password.getText().toString(),email.getText().toString());
if(isUpdate == true)
Toast.makeText(MainActivity.this,
"Data Update",Toast.LENGTH_LONG).show();
else Toast.makeText(MainActivity.this,
"Data not Updated",Toast.LENGTH_LONG).show();
}
public void delete(View view) {
Integer deletedRows = myDb.deleteData(uid.getText().toString());
if(deletedRows > 0)
Toast.makeText(MainActivity.this,
"Data Deleted",Toast.LENGTH_LONG).show();
else Toast.makeText(MainActivity.this,
"Data not Deleted",Toast.LENGTH_LONG).show();
}
}
Helper class:
package com.example.basha.sqlitesimplecurd;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "useraccount.db";
public static final String TABLE_NAME = "user_table";
public static final String COL_1 = "UID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PASSWORD";
public static final String COL_4 = "EMAIL";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +
" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
OUTPUT:
Thank you for sharing this blog which contains a lot of information.
ReplyDeleteUI Development Online Training
Angular JS Training
MEAN Stack Training