database in android
DESCRIPTION
Database in Android pplication development.TRANSCRIPT
![Page 1: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/1.jpg)
DATABASE IN ANDROIDFIRST PART
![Page 2: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/2.jpg)
If you want the app to remember data between
sessions, you need to persist the data.
How?
You can store your data using a SQLite
database.
Database Persistence
![Page 3: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/3.jpg)
SQLite is a lightweight Database that :
● Use simple file database to store data.
● Can use SQL for Database operations.
● Can manage transactions.
● Specifications and source code are being published
● Driver have been developed in many languages.
What is SQLite?
![Page 4: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/4.jpg)
● Thus, you can create and open database directly inside your app.
● The best way to get off the ground with a new database is to extend
a built in abstract base class called SQLiteOpenHelper that provides
you with all of the basic behavior to manage a database.
Start by creating the database
![Page 5: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/5.jpg)
Create Database Perform:
Tables search/ insert/renew/delete
Create Table
Perform database connection: Execute the specified SQL-
(open/close)
Classes provided by Android
SQLiteHelper Class SQLiteDatabase Class
![Page 6: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/6.jpg)
1. Create subclass of SQLiteOpenHelper class.
The method of Creation DB
![Page 7: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/7.jpg)
2.Execute the constructor of SQLiteOpenHelper class by
constructor of the class created in 1, create database.
The method of Creation DB
![Page 8: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/8.jpg)
3. Implement 2 methods that are decided by
SQLiteOpenHelper:
● onCreate: The method that implements the table creation
process.
● onUpgrade: The table that implements the table definition
renew process.
The method of creation DB
![Page 9: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/9.jpg)
4. Execute the SQL statement that creates Table at
SQLiteOpenHelper#onCREATE.
The method of creation DB
![Page 10: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/10.jpg)
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.log;
public class TrySQLiteOpenHelper extends SQLiteOpenHelper{
public trySQLiteOpenHelper(Context context){
super(context,”SAMPLE_DATABASE”, null, 1);
}
Example:
![Page 11: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/11.jpg)
@override
public void onCreate(SQLiteDatabase database){
database.exec.SQL(“CREATE_TABLE_SAMPLE_TABLE
(-idINTEGER,
nameTT>T);”);
}
Note:
exec.SQL() executes the optional SQL statement to operate data.
Example(continue):
![Page 12: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/12.jpg)
@Override
public void onUpgrade
(SQLite Database, int old version, int new version){
}
}
Example(continue):
![Page 13: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/13.jpg)
The mechanism is :
● Need SQLiteDatabase objects to search data from database.
● By executing SQLiteDatabase#query method, the results can be
gotten by Cursor class.
Note:
Cursor is the class which represents a 2 dimensional table of any
database.
Search data from DB
![Page 14: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/14.jpg)
● Get SQliteOpenDatabase objects by SQLiteOpenHelper method.
And, depend on uses, There’re 2 methods:
● getReadableDatabase.
● getWritableDatabase.
Search data from DB
![Page 15: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/15.jpg)
● The method of search data:
1. Create the object of SQLiteOpenHelper class.
2. Use SQLiteOpenHelper#getReadable Database method to get
SQLiteDatabase objects.
3. Execute SQLiteDatabase#query method to get the Cursor objects
of search results.
4. Execute Activity#startManagingCursor to transmit Cursor
management to Activity.
5. Execute SQLiteDatabase#close method to close database.
SQLiteDatabase#query method
![Page 16: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/16.jpg)
TrySQLiteOpenHelper database;
OpenHelper = new TrySQLiteOpenHelper(this);
SQLiteDatabase database = null;
Cursor cursor= null;
try{
// get the SQLiteDatabase objects.
database = database.OpenHelper.getReadableDatabase();
//get the cursor object of searching results.
cursor = database.query(“SAMPLE_TABLE”,null, null, null, null,
null);
Example(query method)
![Page 17: Database in Android](https://reader034.vdocuments.mx/reader034/viewer/2022052400/559be20e1a28ab3c168b45c0/html5/thumbnails/17.jpg)
// Transfer the Cursor object management to Activity
startManagingCursor( cursor);
}finally{
if(database)= null){
//disconnect with database
database.close();
}
}
Example( query method)