Lab Android part 7
SQLite Database
SQLite in Android
• SQLite is a Structure query base database, open source, light weight,
no network access and standalone database. It support embedded
relational database features.
• The database created is saved in a directory:
data/data/APP_Name/databases/DATABASE_NAME.
• /data/data/<package_name>/databases
1. Run cmd as administrator and
cd users\username\AppData\Local\Android\sdk\platform-tools
2. C:\Users\username\AppData\Local\Android\sdk\platform-tools>adb devices
cmd: List of device
emulator-xxxx device ------ > This is your device and then,
3. C:\Users\username\AppData\Local\Android\sdk\platform-tools>adb -s emulator-xxxx shell
4. - shell: generic_x86:/ cd <package_name for example
(com.bignerdranch.android.criminalintent)>
5. generic_x86:/data/data/com.bignerdranch.android.criminalintent cd databases
cache databases
6. generic_x86:/data/data/com.bignerdranch.android.criminalintent/databases $ ls
crimeBase.db crimeBase.db-journal
Database Functions
SQLiteOpenHelper class
• 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) &
onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion)
• 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.
Need to create a
class
myDBAdapter()
Function addUser ()
Need to create a
class Message()
Function viewdata ()
Function update ()
Function
delete ()
Create a new
class namely
myDBAdapter
Update XML files for all
buttons
Add the following code “onClick” for each button
android:text="Add Data“ android:text="View Data“
Put this code: Put this code:
android:onClick="addUser" android:onClick="viewdata"
android:text="Delete Name“
android:text="Update Name“
Put this code:
Put this code:
android:onClick="delete"
android:onClick="update"
Some got this, some ok, no worries
Emulator: Nexus 6, API 27 (bigger version)
Add Data Operation
Record starts from 3, because
I deleted record 1 and 2 before.
Modify / Update Name Operation
Delete Operation
More Complex Coding SQLite Database
We will create a database with a spinner, a listview and
dialogs
We need 4 XML files
2 new layout activities, will auto-generate related Java file
(activity_employee.xml)
(activity_main.xml)
2 XML layout (without activity), will not generate Java file
Or we call it stand_alone XML layout file
(dialog_update_employee.xml)
(list_layout_employee.xml)
We need 5 Java files
2 is generated from layout activities
(EmployeeActivity.java)
(MainActivity.Java)
3 is new Java class (stand-alone Java class)
(DatabaseManager.Java)
(Employee.Java)
(Employee Adapter.Java)
Let us create ALL XML files first there
are 4 of them
Activity_main.xml
Do all, but spinner.
Spinner code on
next slide
Activity_main.xml
Reuse this spinner code
<Spinner
android:id="@+id/spinnerDepartment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Go here after you android:entries="@array/departments" />
complete the design
Reuse this code for string.xml
<resources>
<string name="app_name">MyDatabaseManager</string>
<array name="departments">
<item>Technical</item>
<item>Support</item>
<item>Research and Development</item>
<item>Marketing</item>
<item>Human Resource</item>
</array>
</resources>
Activity_main.xml is associated with MainActivity.Java.
Let us do coding for MainActivity.Java
MainActivity.Java
MainActivity.Java
MainActivity.Java
Go to res, layout, right click, new, activity, empty activity, name it Activity_employee
Activity_employee.xml
This activity_employee will auto-generate a Java file, namely EmployeeActivity.java
EmployeeActivity.java
EmployeeActivity.java
OK, by this time, we have done 2
XML activities with the associated 2
Java files
Now, we need to create : 3 standalone Java files and 2 standalone XML layout files
Go to res, layout, right click, new, XML, Layout XML file, put name: list_layout_employee
Do this design by yourself,
Follow the linear layout,
Put name exactly same,
To avoid mistake in the java
coding.
Go to res, layout, right click, new, XML, Layout XML file, put name: dialog_update_employee
This design is almost same with
activity_main.xml
The difference is the top TextView, it is
written “Edit Employee” in here.
And Button is written Update in here.
So, you can reuse the previous code, just
change the textview into “Edit Employee”
and button is “Update”
Previous codes inside dialog_update_employee
Will give you this interface for Edit Employee
This is dialog.
OK, by now, we have done ALL 4 XML files with 2
Java files.
Next, we need to do 3 stand-alone Java files.
Go to java folder, com.example folder, right click, new Java class, put name Employee
Employee.Java
Type this code by yourself.
Employee.Java
Complete your code until finish.
Go to java folder, com.example folder, right click, new Java class,
put name DatabaseManager
DatabaseManager.Java
Type this code by yourself.
Complete your code until finish.
DatabaseManager.Java
Complete your code until finish.
DatabaseManager.Java
Complete your code until finish.
DatabaseManager.Java
Go to java folder, com.example folder, right click, new Java class,
put name EmployeeAdapter
EmployeeAdapter.Java
Type this code by yourself.
Continue your code until finish
EmployeeAdapter.Java
Continue your code until finish
EmployeeAdapter.Java
Continue your code until finish
EmployeeAdapter.Java
Continue your code until finish
EmployeeAdapter.Java
Continue your code until finish
EmployeeAdapter.Java
Allright,
we have done all 9 files
Please check again, have
you done all?
Emulator: Nexus 6, API 27
(bigger version) Add Data
View Data
We can see ListView is
shown
Delete Karl
Edit Mamamia
The End