Databases in Android Application
-
Upload
lestervirus -
Category
Mobile
-
view
185 -
download
0
description
Transcript of Databases in Android Application
![Page 1: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/1.jpg)
USING SQLITEDatabases in Android Application
![Page 2: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/2.jpg)
Objectives Know more about SQLite. Understand the process on how to create
database in an Android application using SQLite.
Use SQLite commands. Enhance Android programming skills by
incorporating database in an Android app.
![Page 3: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/3.jpg)
Know More About SQLite. SQLite is an Open Source database that
supports standard relational database features like SQL syntax, transactions and prepared statements.
The database requires limited memory at runtime (approx. 250 Kbyte)
SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar to long in Java) and REAL (similar to double in Java).
![Page 4: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/4.jpg)
Know More About SQLite. Uses a wrapper
class, SQLiteOpenHelper which offers three basic API methods to interact with the database: onCreate(SQLiteDatabase db), called when the
database is created for the first time. onOpen(SQLiteDatabase db), called when the
database has been opened. onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion), called when the database needs to be upgraded.
![Page 5: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/5.jpg)
Know More About SQLite. The “Lite” in SQLite does not refer to its
capabilities. Rather, SQLite is lightweight when it comes to setup complexity, administrative overhead, and resource usage.
Characteristics: Server less Zero Configuration Self-Contained Full-featured
![Page 6: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/6.jpg)
Creating Database in an Android App using SQLite
1. Design your database.
Id Title Author
1 Android Database Henry Williams
2 Philippine History Jose Natividad
Books
![Page 7: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/7.jpg)
Creating Database in an Android App using SQLite
2. Create a new Android Application project and design your UI.
![Page 8: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/8.jpg)
Creating Database in an Android App using SQLite
3. Add a class in the project that extends the SQLOpenHelper wrapper class.
![Page 9: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/9.jpg)
Creating Database in an Android App using SQLite
4. Doing so, will let you import necessary packages:
5. Then add the database name and the database version.
![Page 10: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/10.jpg)
Creating Database in an Android App using SQLite
6. Add the constructor
![Page 11: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/11.jpg)
Creating Database in an Android App using SQLite7. Add these two API methods:
![Page 12: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/12.jpg)
Adding a Record to the Database1. To add a record, you need to create a
method for that, but declare variables first:
![Page 13: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/13.jpg)
Adding a Record to the Database2. Add the addBook method, with this you
need to import ContentValues
![Page 14: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/14.jpg)
Searching and Retrieving Data from the Database1. To search and retrieve data, you will
have to use Cursor class.
![Page 15: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/15.jpg)
Implementing Database1. On the MainActivity, add a method for
getting user input and adding it to the database
![Page 16: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/16.jpg)
Implementing Database2. Add another method, this time is
searching for a book’s author based on the input title.
![Page 17: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/17.jpg)
Implementing Database3. Set onClick on the xml properties of the Add
and Search buttons.4. Run your app, fix bugs for errors. Add and
search for the ff contents:
Title AuthorAndroid Database Henry
WilliamsPhilippine History Jose Natividad
![Page 18: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/18.jpg)
Laboratory Exercise:1. Open your PC, register to NetSupport.2. Open Exercise4 from your Desktop.3. Read Exercise4 and follow the instructions
carefully.4. Notify your instructor if any problems, or if
done.
![Page 19: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/19.jpg)
Updating Data
public int updateBook(String title, String author) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(key_title, title); values.put(key_author, author); int i = db.update(tblName, //table values, // column/value "title=?", // selections new String[] { String.valueOf(title) }); //selection args db.close(); return i;}
![Page 20: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/20.jpg)
public void updateBookClick(View v){MySQLiteHelper db=new MySQLiteHelper(this);
EditText txtTitle=(EditText) findViewById(R.id.editText1); EditText txtAuthor=(EditText) findViewById(R.id.editText2); if ((txtTitle.getText().toString()).equalsIgnoreCase("")) Toast.makeText(getApplicationContext(), "Invalid, null field.",
Toast.LENGTH_SHORT).show(); else { int i= db.updateBook(txtTitle.getText().toString(),
txtAuthor.getText().toString()); Toast.makeText(getApplicationContext(), i + " updated",
Toast.LENGTH_SHORT).show(); txtTitle.setText(""); txtAuthor.setText(""); } }
![Page 21: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/21.jpg)
Deleting Data
public void deleteBook(String title) { SQLiteDatabase db = this.getWritableDatabase();
db.delete(tblName, "title=?", new String[] { String.valueOf(title) });
db.close();
}
![Page 22: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/22.jpg)
public void deleteBookClick(View v){ MySQLiteHelper db=new MySQLiteHelper(this); EditText txtTitle=(EditText) findViewById(R.id.editText1);
EditText txtAuthor=(EditText) findViewById(R.id.editText2); if ((txtTitle.getText().toString()).equalsIgnoreCase("")) Toast.makeText(getApplicationContext(), "Invalid, null field.", Toast.LENGTH_SHORT).show(); else { db.deleteBook(txtTitle.getText().toString()); Toast.makeText(getApplicationContext(), "Book deleted", Toast.LENGTH_SHORT).show(); txtTitle.setText(""); txtAuthor.setText(""); }
}
![Page 23: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/23.jpg)
Activity Life Cycle
![Page 24: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/24.jpg)
Activity Life Cycle Resumed - In this state, the activity is in the foreground
and the user can interact with it. (Also sometimes referred to as the "running" state.)
Paused - In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.
Stopped - In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
![Page 25: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/25.jpg)
Activity Life Cycle The other states (Created and Started)
are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method.
That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().
![Page 26: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/26.jpg)
![Page 27: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/27.jpg)
![Page 28: Databases in Android Application](https://reader035.fdocuments.in/reader035/viewer/2022062405/5580fde0d8b42a9d198b4e31/html5/thumbnails/28.jpg)
protected void onStop() { super.onStop(); ContentValues values = new ContentValues(); values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText()); values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update( mUri, values, null, null }
}