<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
" />
activity_main.xml
====================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sam.web.sqliteupdate.MainActivity">
<LinearLayout
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Name"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/nameEditTextId"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Age"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/ageEditTextId"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Gender"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/genderEditTextId"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Id"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/idEditTextId"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:text="ADD Data"
android:id="@+id/addButtonId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:text="Display all Data"
android:id="@+id/displayAllDataButtonId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:text="Update Data"
android:id="@+id/updateDataButtonId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
==========================================================================
MainActivity.java
======================
package com.sam.web.sqliteupdate;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText nameEditText,ageEditText,genderEditText,idEditText;
private Button addButton,displayAllDataButton,updateButton;
MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDatabaseHelper=new MyDatabaseHelper(this);
SQLiteDatabase sqLiteDatabase=myDatabaseHelper.getWritableDatabase();
nameEditText=(EditText) findViewById(R.id.nameEditTextId);
ageEditText=(EditText) findViewById(R.id.ageEditTextId);
genderEditText=(EditText) findViewById(R.id.genderEditTextId);
idEditText=(EditText) findViewById(R.id.idEditTextId);
addButton=(Button) findViewById(R.id.addButtonId);
displayAllDataButton=(Button) findViewById(R.id.displayAllDataButtonId);
updateButton=(Button) findViewById(R.id.updateDataButtonId);
addButton.setOnClickListener(this);
displayAllDataButton.setOnClickListener(this);
updateButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name=nameEditText.getText().toString();
String age=ageEditText.getText().toString();
String gender=genderEditText.getText().toString();
String id=idEditText.getText().toString();
if(view.getId()==R.id.addButtonId){
long rowId=myDatabaseHelper.insertData(name,age,gender);
if (rowId==1){
Toast.makeText(getApplicationContext(),"unsuccessfull",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"Row "+rowId+" is successfully inserted",Toast.LENGTH_LONG).show();
}
}
if (view.getId()==R.id.displayAllDataButtonId){
Cursor cursor=myDatabaseHelper.displayAllData();
if(cursor.getCount()==0){
//if no data will be display message
showData("Error","No data found");
return;
}
StringBuffer stringBuffer=new StringBuffer();
while (cursor.moveToNext()){
stringBuffer.append("ID :"+cursor.getString(0)+" ");
stringBuffer.append("Name :"+cursor.getString(1)+" ");
stringBuffer.append("Age :"+cursor.getString(2)+" ");
stringBuffer.append("Gender :"+cursor.getString(3)+" ");
}
showData("resultSet",stringBuffer.toString());
}else if(view.getId()==R.id.updateDataButtonId){
Boolean isUpdated= myDatabaseHelper.updateData(id,name,age,gender);
if(isUpdated==true){
Toast.makeText(getApplicationContext(),"Data is updated",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"Data is no updated",Toast.LENGTH_LONG).show();
}
}
}
public void showData(String title,String message){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.show();
}
}
============================================================================================================
MyDatabaseHelper.java
=======================
package com.sam.web.sqliteupdate;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by maudud on 1/31/18.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="Student" +
".db";
private static final String TABLE_NAME="student_details";
private static final String ID="_id";
private static final String NAME="Name";
private static final String AGE="Age";
private static final String GENDER="Gender";
private static final int VERSION_NUMBER=1;
private static final String CREATE_TABLE= "CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),"+AGE+" INTEGER, "+GENDER+" VARCHAR(15));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
private static final String SELECT_ALL="SELECT * FROM"+TABLE_NAME;
private Context context;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION_NUMBER);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
try {
Toast.makeText(context," onCreate is called",Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL(CREATE_TABLE);
}catch (Exception e){
Toast.makeText(context,"Exception :"+e,Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
try {
Toast.makeText(context," onUpgrade is called",Toast.LENGTH_LONG).show();
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}catch (Exception e){
Toast.makeText(context,"Exception :"+e,Toast.LENGTH_LONG).show();
}
}
public long insertData(String name,String age, String gender){
SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(NAME,name);
contentValues.put(AGE,age);
contentValues.put(GENDER,gender);
long rowId=sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
return rowId;
}
public Cursor displayAllData(){
SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery(SELECT_ALL,null);
return cursor;
}
public boolean updateData(String id,String name,String age,String gender){
SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(ID,id);
contentValues.put(NAME,name);
contentValues.put(AGE,age);
contentValues.put(GENDER,gender);
sqLiteDatabase.update(TABLE_NAME,contentValues,ID+" =?",new String[]{id});
return true;
}
}