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.
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).
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.
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
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
Creating Database in an Android App using SQLite 2. Create a new Android Application project and design your UI.
Creating Database in an Android App using SQLite 3. Add a class in the project that extends the SQLOpenHelper wrapper class.
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.
Creating Database in an Android App using SQLite 6. Add the constructor
Creating Database in an Android App using SQLite 7. Add these two API methods:
Adding a Record to the Database 1. To add a record, you need to create a method for that, but declare variables first:
Adding a Record to the Database 2. Add the addBook method, with this you need to import ContentValues
Searching and Retrieving Data from the Database 1. To search and retrieve data, you will have to use Cursor class.
Implementing Database 1. On the MainActivity , add a method for getting user input and adding it to the database
Implementing Database 2. Add another method, this time is searching for a book’s author based on the input title.
Implementing Database 3. 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 Author Android Database Henry Williams Philippine History Jose Natividad
Laboratory Exercise: Open your PC, register to NetSupport . Open Exercise4 from your Desktop. Read Exercise4 and follow the instructions carefully. Notify your instructor if any problems, or if done.
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 ; }
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.
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 () .