Nishant Seth
TYCS B
8879
Practical 8
1. Aim: Create an android application to store the name and
age entered by user into database.
Program:
DBHelper.java
package com.example.adpractical8;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
static String dbname = "details";
static int version = 1;
public DBHelper(Context context) {
super(context, dbname, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT, age INTEGER)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
Nishant Seth
TYCS B
8879
activity_main.java
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="16dp"
android:hint="Enter Age"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/age"
android:layout_marginTop="16dp"
android:text="Add to Database"/>
MainActivity.java
package com.example.adpractical8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText nameEdit, ageEdit;
Button btn;
DBHelper help = new DBHelper(this);
@Override
Nishant Seth
TYCS B
8879
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEdit = findViewById(R.id.name);
ageEdit = findViewById(R.id.age);
btn = findViewById(R.id.btn);
btn.setOnClickListener(v -> {
String name = nameEdit.getText().toString().trim();
String a = ageEdit.getText().toString().trim();
if (!name.isEmpty() && !a.isEmpty()){
int age = Integer.parseInt(a);
SQLiteDatabase db = help.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("age", age);
long rowId = db.insert("users", null, cv);
if (rowId != -1){
nameEdit.setText("");
ageEdit.setText("");
Toast.makeText(this, "Work Done!!!", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
db.close();
}
});
}
}
Nishant Seth
TYCS B
8879
Output:
Nishant Seth
TYCS B
8879
2. Aim: Create an android application to create new emp
table in SQLite with fields as name, age and designation then
insert records in it and show the records on screen.
Program:
DBHelper.java
package com.example.adpractical8;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
static String dbname = "employee";
static int version = 1;
public DBHelper(Context context) {
super(context, dbname, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE emp(id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT, age INTEGER, designation TEXT)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS emp");
onCreate(db);
}
}
activity_main.xml
Nishant Seth
TYCS B
8879
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="16dp"
android:hint="Enter Age"/>
<EditText
android:id="@+id/designation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/age"
android:layout_marginTop="16dp"
android:hint="Enter Designation"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/designation"
android:layout_marginTop="16dp"
android:text="Add to Database"/>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_marginTop="16dp"
android:text="Show Records"/>
<TextView
android:id="@+id/rec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/show"/>
Nishant Seth
TYCS B
8879
MainActivity.java
package com.example.adpractical8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText nameEdit, ageEdit, designationEdit;
Button add, show;
TextView txt;
DBHelper help = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEdit = findViewById(R.id.name);
ageEdit = findViewById(R.id.age);
designationEdit = findViewById(R.id.designation);
add = findViewById(R.id.add);
show = findViewById(R.id.show);
txt = findViewById(R.id.rec);
add.setOnClickListener(v -> {
String name = nameEdit.getText().toString().trim();
String a = ageEdit.getText().toString().trim();
String designation = designationEdit.getText().toString().trim();
if (!name.isEmpty() && !a.isEmpty()){
int age = Integer.parseInt(a);
SQLiteDatabase db = help.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
Nishant Seth
TYCS B
8879
cv.put("age", age);
cv.put("designation", designation);
long rowId = db.insert("emp", null, cv);
if (rowId != -1){
nameEdit.setText("");
ageEdit.setText("");
designationEdit.setText("");
Toast.makeText(this, "Work Done!!!", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
db.close();
}
else
Toast.makeText(this, "Please fill the data in required fields",
Toast.LENGTH_SHORT).show();
});
show.setOnClickListener(v -> {
SQLiteDatabase db = help.getReadableDatabase();
StringBuilder rec = new StringBuilder();
String[] proj = {"name", "age", "designation"};
Cursor c = db.query("emp", proj, null, null, null, null, null);
while (c.moveToNext()){
String name = c.getString(c.getColumnIndexOrThrow("name"));
int age = c.getInt(c.getColumnIndexOrThrow("age"));
String designation = c.getString(c.getColumnIndexOrThrow("designation"));
rec.append("Name: ").append(name).append("\t\tAge: ")
.append(age).append("\t\tDesignation: ").append(designation).append("\n");
}
c.close();
db.close();
txt.setText(rec);
});
Nishant Seth
TYCS B
8879
}
}
Output:
Nishant Seth
TYCS B
8879
3. Aim: Create and android application to show the spinner
while adding data into the database.
Program:
DBHelper.java
package com.example.adpractical8;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
static String dbname = "employee";
static int version = 1;
public DBHelper(Context context) {
super(context, dbname, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE emp(id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT, age INTEGER, designation TEXT)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS emp");
onCreate(db);
}
}
Nishant Seth
TYCS B
8879
activity_main.xml
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="16dp"
android:hint="Enter Age"/>
<EditText
android:id="@+id/designation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/age"
android:layout_marginTop="16dp"
android:hint="Enter Designation"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/designation"
android:layout_marginTop="16dp"
android:text="Add to Database"/>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_marginTop="16dp"
android:text="Show Records"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Nishant Seth
TYCS B
8879
android:layout_below="@id/show"
android:layout_marginTop="16dp"
android:visibility="gone"/>
<TextView
android:id="@+id/rec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/show"/>
MainActivity.java
package com.example.adpractical8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText nameEdit, ageEdit, designationEdit;
Button add, show;
TextView txt;
ProgressBar pb;
DBHelper help = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEdit = findViewById(R.id.name);
ageEdit = findViewById(R.id.age);
designationEdit = findViewById(R.id.designation);
Nishant Seth
TYCS B
8879
add = findViewById(R.id.add);
show = findViewById(R.id.show);
pb = findViewById(R.id.progress);
txt = findViewById(R.id.rec);
add.setOnClickListener(v -> new AddToDatabaseTask().execute());
show.setOnClickListener(v -> {
SQLiteDatabase db = help.getReadableDatabase();
StringBuilder rec = new StringBuilder();
String[] proj = {"name", "age", "designation"};
Cursor c = db.query("emp", proj, null, null, null, null, null);
while (c.moveToNext()){
String name = c.getString(c.getColumnIndexOrThrow("name"));
int age = c.getInt(c.getColumnIndexOrThrow("age"));
String designation = c.getString(c.getColumnIndexOrThrow("designation"));
rec.append("Name: ").append(name).append("\t\tAge: ")
.append(age).append("\t\tDesignation: ").append(designation).append("\n");
}
c.close();
db.close();
txt.setText(rec);
});
}
private class AddToDatabaseTask extends AsyncTask<Void, Void, Boolean>{
@Override
protected void onPreExecute() {
setAddingRecords(true);
pb.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(Void... params) {
try{
Thread.sleep(2000);
}
catch (InterruptedException e) {
Nishant Seth
TYCS B
8879
e.printStackTrace();
}
String name = nameEdit.getText().toString().trim();
String a = ageEdit.getText().toString().trim();
String designation = designationEdit.getText().toString().trim();
if (!name.isEmpty() && !a.isEmpty() && !designation.isEmpty()){
int age = Integer.parseInt(a);
SQLiteDatabase db = help.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("age", age);
cv.put("designation", designation);
long rowId = db.insert("emp", null, cv);
db.close();
return rowId != -1;
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
pb.setVisibility(View.GONE);
setAddingRecords(false);
if (aBoolean){
nameEdit.setText("");
ageEdit.setText("");
designationEdit.setText("");
showToast("Record Added Successfully!!!");
}
else
showToast("Error Adding Record....");
}
Nishant Seth
TYCS B
8879
private boolean isAddingRecords(){
return pb.getVisibility() == View.VISIBLE;
}
private void setAddingRecords(boolean b) {
if (b){
add.setEnabled(false);
show.setEnabled(false);
}
else{
add.setEnabled(true);
show.setEnabled(true);
}
private void showToast(String s) {
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
}
Nishant Seth
TYCS B
8879
Output: