CPET - 15L - Lab6
CPET - 15L - Lab6
Name: ___________________________________________
Manila Campus
Course/Year/Section: _______________________________
College of Industrial Technology
Instructor:___________________________________
Electronics Department
Date Started: ____________ Date Submitted:___________
Corrected by: _______________________ Rating: _______
ANDROID STUDIO
Finals (1) Laboratory 6
I. OBJECTIVES
At the end of the lesson, the students are expected to:
• Understand the Mobile Development environment to familiarize
• Design simple GUI application with activity and intents e.g. calculator.
• Create an android app for database creation usingSQLite Database.
1. MATERIALS
Pen and Notebook
Laptop
Cellphone
Android studio Others:____________________
2. GENERAL INSTRUCTION
SQLite Database.
SQLite
SQLite is a Structure query base database, open source, light weight, no network access and
standalone database. It support embedded relational database features.
Whenever an application needs to store large amount of data then using sqlite is more
preferable than other repository system like Shared Preferences or saving data in files.
Android has built in SQLite database implementation. It is available locally over the
device(mobile & tablet) and contain data in text format. It carry light weight data and suitable
with many languages. So, it doesn’t required any administration or setup procedure of the
database.
Important Note – The database created is saved in a directory:
data/data/APP_Name/databases/DATABASE_NAME.
Creating And Updating Database In Android
For creating, updating and other operations you need to create a subclass or
SQLiteOpenHelper class. SQLiteOpenHelper is a helper class to manage database creation and
version management.
It provides two methods:
• onCreate(SQLiteDatabase db),
• onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion).
The SQLiteOpenHelper is responsible for opening database if exist, creating database if it does
not exists and upgrading if required. The SQLiteOpenHelper only require the
DATABASE_NAME to create database. After extending SQLiteOpenHelper you will need to
implement its methods onCreate, onUpgrade and constructor.
onCreate(SQLiteDatabase sqLiteDatabase) method is called only once throughout the
application lifecycle. It will be called whenever there is a first call to getReadableDatabase()
or getWritableDatabase() function available in super SQLiteOpenHelper
class. So SQLiteOpenHelper class call the onCreate() method after creating database and
instantiate SQLiteDatabase object. Database name is passed in constructor call.
onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion) is only called whenever there
is a updation in existing version. So to update a version we have to increment the value of
version variable passed in the superclass constructor.
In onUpgrade method we can write queries to perform whatever action is required. In most
example you will see that existing table(s) are being dropped and again onCreate() method is
being called to create tables again. But it’s not mandatory to do so and it all depends upon your
.
• Step 3 − Create a folder C:\>sqlite and unzip above two zipped files in this folder, which
will give you sqlite3.def, sqlite3.dll and sqlite3.exe files.
• Step 4 − Add C:\>sqlite in your PATH environment variable and finally go to the command
prompt and issue sqlite3 command, which should display the following result.
C:\>sqlite3
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";" sqlite>
Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
?
1 <?xml version="1.0" encoding="utf-8"?>
2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent">
5 <TextView
6 android:layout_width="wrap_content"
7 android:layout_height="wrap_content"
8 android:layout_x="50dp"
9 android:layout_y="20dp"
10 android:text="Student Details"
11 android:textSize="30sp" /> 12
13 <TextView
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:layout_x="20dp"
17 android:layout_y="110dp"
18 android:text="Enter Rollno:"
19 android:textSize="20sp" /> 20
21 <EditText
22 android:id="@+id/Rollno"
23 android:layout_width="150dp"
24 android:layout_height="wrap_content"
25 android:layout_x="175dp"
26 android:layout_y="100dp"
27 android:inputType="number"
28 android:textSize="20sp" /> 29
30 <TextView
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:layout_x="20dp"
34 android:layout_y="160dp"
35 android:text="Enter Name:"
36 android:textSize="20sp" /> 37
38 <EditText
39 android:id="@+id/Name"
40 android:layout_width="150dp"
41 android:layout_height="wrap_content"
42 android:layout_x="175dp"
43 android:layout_y="150dp"
44 android:inputType="text"
45 android:textSize="20sp" /> 46
47 <TextView
48 android:layout_width="wrap_content"
49 android:layout_height="wrap_content"
50 android:layout_x="20dp"
51 android:layout_y="210dp"
52 android:text="Enter Marks:"
53 android:textSize="20sp" /> 54
55 <EditText
56 android:id="@+id/Marks"
57 android:layout_width="150dp"
58 android:layout_height="wrap_content"
59 android:layout_x="175dp"
60 android:layout_y="200dp"
61 android:inputType="number"
62 android:textSize="20sp" /> 63
64 <Button
65 android:id="@+id/Insert"
66 android:layout_width="150dp"
67 android:layout_height="wrap_content"
68 android:layout_x="25dp"
69 android:layout_y="300dp"
70 android:text="Insert"
71 android:textSize="30dp" /> 72
73 <Button
74 android:id="@+id/Delete"
75 android:layout_width="150dp"
76 android:layout_height="wrap_content"
77 android:layout_x="200dp"
78 android:layout_y="300dp"
79 android:text="Delete"
80 android:textSize="30dp" /> 81
82 <Button
83 android:id="@+id/Update"
84 android:layout_width="150dp"
85 android:layout_height="wrap_content"
86 android:layout_x="25dp"
87 android:layout_y="400dp"
88 android:text="Update"
89 android:textSize="30dp" /> 90
91 <Button
92 android:id="@+id/View"
93 android:layout_width="150dp"
94 android:layout_height="wrap_content"
95 android:layout_x="200dp"
96 android:layout_y="400dp"
97 android:text="View"
98 android:textSize="30dp" /> 99
100 <Button
101 android:id="@+id/ViewAll"
102 android:layout_width="200dp"
103 android:layout_height="wrap_content"
104 android:layout_x="100dp"
105 android:layout_y="500dp"
106 android:text="View All"
107 android:textSize="30dp" /> 108
109 </AbsoluteLayout>
Now click on Design and your application will look as given below.
1 package com.example.exno5; 2
3 import android.app.Activity;
4 import android.app.AlertDialog.Builder;
5 import android.content.Context;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.EditText; 13
14 public class MainActivity extends Activity implements OnClickListener 15 {
16 EditText Rollno,Name,Marks;
17 Button Insert,Delete,Update,View,ViewAll;
18 SQLiteDatabase db;
19 /** Called when the activity is first created. */
20 @Override
21 public void onCreate(Bundle savedInstanceState) 22 {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_main); 25
26 Rollno=(EditText)findViewById(R.id.Rollno);
27 Name=(EditText)findViewById(R.id.Name);
28 Marks=(EditText)findViewById(R.id.Marks);
29 Insert=(Button)findViewById(R.id.Insert);
30 Delete=(Button)findViewById(R.id.Delete);
31 Update=(Button)findViewById(R.id.Update);
32 View=(Button)findViewById(R.id.View);
33 ViewAll=(Button)findViewById(R.id.ViewAll); 34
35 Insert.setOnClickListener(this);
36 Delete.setOnClickListener(this);
37 Update.setOnClickListener(this);
38 View.setOnClickListener(this);
39 ViewAll.setOnClickListener(this); 40
41 // Creating database and table
42 db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
43 db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name
44 VARCHAR,marks VARCHAR);"); 45 }
57 return;
58 }
59 db.execSQL("INSERT INTO student
60 VALUES('"+Rollno.getText()+"','"+Name.getText()+
61 "','"+Marks.getText()+"');");
62 showMessage("Success", "Record added");
63 clearText();
64 }
65 // Deleting a record from the Student table
66 if(view==Delete)
67 {
68 // Checking for empty roll number
69 if(Rollno.getText().toString().trim().length()==0)
70 {
71 showMessage("Error", "Please enter Rollno");
72 return;
73 }
74 Cursor c=db.rawQuery("SELECT * FROM student WHERE
75 rollno='"+Rollno.getText()+"'", null);
76
77
78
79
80
81
82
83
84
85
86 }
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
106 }
107 clearText();
108 }
109 // Display a record from the Student table
110 if(view==View)
111 {
112 // Checking for empty roll number
113 if(Rollno.getText().toString().trim().length()==0)
114 {
115 showMessage("Error", "Please enter Rollno");
116 return;
117 }
118 Cursor c=db.rawQuery("SELECT * FROM student WHERE
119 rol 120 lno='"+Rollno.getText()+"'", null); if(c.moveToFirst())
121 {
122 Name.setText(c.getString(1));
123 Marks.setText(c.getString(2));
124 }
125 else
126 {
127 showMessage("Error", "Invalid Rollno");
128 clearText();
129 }
130 }
131 // Displaying all the records
132 if(view==ViewAll)
133 {
134 Cursor c=db.rawQuery("SELECT * FROM student", null);
135 if(c.getCount()==0)
136 {
137 showMessage("Error", "No records found");
138 return;
139 }
140 StringBuffer buffer=new StringBuffer();
141 while(c.moveToNext())
142 {
143 buffer.append("Rollno: "+c.getString(0)+"\n");
144 buffer.append("Name: "+c.getString(1)+"\n");
145 buffer.append("Marks: "+c.getString(2)+"\n\n");
146 }
147 showMessage("Student Details", buffer.toString());
148 }
149 }
150 public void showMessage(String title,String message)
151 {
152 Builder builder=new Builder(this);
153 builder.setCancelable(true);
154 builder.setTitle(title);
155 builder.setMessage(message);
156 builder.show();
157 }
158 public void clearText()
159 {
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
Output:
Result:
Thus a Simple Android Application that makes use of
Database is developed andexecuted successfully.
Activity 1.1
For your next lab activity please try to change the ff:
1. Redesign the the inputs: Create Student basic profile
2. Include your full name, student no. and enrolled subjects
Note all activities made must have a screen shots all procedure you done your codes and output.
Output needed:
A. Default Output
B. Activity 1.1