Simple Android Application that makes
use of Database
Aim:
To develop a Simple Android Application that makes use of Database.
Procedure:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “[Link].4″ and click Next.
Then select the Empty Activity and click Next.
Finally click Finish.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
Code for Activity_main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Code for [Link]:
package [Link].exno5;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity implements OnClickListener
{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
Rollno=(EditText)findViewById([Link]);
Name=(EditText)findViewById([Link]);
Marks=(EditText)findViewById([Link]);
Insert=(Button)findViewById([Link]);
Delete=(Button)findViewById([Link]);
Update=(Button)findViewById([Link]);
View=(Button)findViewById([Link]);
ViewAll=(Button)findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
// Creating database and table
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
[Link]("CREATE TABLE IF NOT EXISTS student(rollno
VARCHAR,name VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if([Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
[Link]("INSERT INTO student
VALUES('"+[Link]()+"','"+[Link]()+
"','"+[Link]()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]("DELETE FROM student WHERE
rollno='"+[Link]()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]()) {
[Link]("UPDATE student SET name='" + [Link]() +
"',marks='" + [Link]() +
"' WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]([Link](1));
[Link]([Link](2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=[Link]("SELECT * FROM student", null);
if([Link]()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while([Link]())
{
[Link]("Rollno: "+[Link](0)+"\n");
[Link]("Name: "+[Link](1)+"\n");
[Link]("Marks: "+[Link](2)+"\n\n");
}
showMessage("Student Details", [Link]());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
[Link](true);
[Link](title);
[Link](message);
[Link]();
}
public void clearText()
{
[Link]("");
[Link]("");
[Link]("");
[Link]();
}
}