Lab Android Part 7 SQLite Database
Lab Android Part 7 SQLite Database
Lab Android Part 7 SQLite Database
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)
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="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)
(activity_employee.xml)
(activity_main.xml)
(dialog_update_employee.xml)
(list_layout_employee.xml)
We need 5 Java files
2 is generated from layout activities
(EmployeeActivity.java)
(MainActivity.Java)
Activity_main.xml
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
This is dialog.
OK, by now, we have done ALL 4 XML files with 2
Java files.
Employee.Java
DatabaseManager.Java
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