Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Database in Android

1,742 views

Published on

Database in Android pplication development.

Published in: Technology
  • Login to see the comments

Database in Android

  1. 1. DATABASE IN ANDROID FIRST PART
  2. 2. Database Persistence 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.
  3. 3. What is SQLite? 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.
  4. 4. Start by creating the database ● 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.
  5. 5. Classes provided by Android SQLiteHelper Class SQLiteDatabase Class Create Database Perform: Tables search/ insert/renew/delete Create Table Perform database connection: Execute the specified SQL- (open/close)
  6. 6. The method of Creation DB 1. Create subclass of SQLiteOpenHelper class.
  7. 7. The method of Creation DB 2.Execute the constructor of SQLiteOpenHelper class by constructor of the class created in 1, create database.
  8. 8. The method of creation DB 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.
  9. 9. The method of creation DB 4. Execute the SQL statement that creates Table at SQLiteOpenHelper#onCREATE.
  10. 10. 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:
  11. 11. @override Example(continue): 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.
  12. 12. @Override Example(continue): public void onUpgrade (SQLite Database, int old version, int new version){ } }
  13. 13. Search data from DB 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.
  14. 14. Search data from DB ● Get SQliteOpenDatabase objects by SQLiteOpenHelper method. And, depend on uses, There’re 2 methods: ● getReadableDatabase. ● getWritableDatabase.
  15. 15. SQLiteDatabase#query method ● 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.
  16. 16. 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)
  17. 17. // Transfer the Cursor object management to Activity startManagingCursor( cursor); }finally{ if(database)= null){ //disconnect with database database.close(); } } Example( query method)

×