Upload
lestervirus
View
186
Download
0
Tags:
Embed Size (px)
DESCRIPTION
SQLite Database in Android Application
Citation preview
USING SQLITEDatabases in Android Application
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 SQLite7. Add these two API methods:
Adding a Record to the Database1. To add a record, you need to create a
method for that, but declare variables first:
Adding a Record to the Database2. Add the addBook method, with this you
need to import ContentValues
Searching and Retrieving Data from the Database1. To search and retrieve data, you will
have to use Cursor class.
Implementing Database1. On the MainActivity, add a method for
getting user input and adding it to the database
Implementing Database2. Add another method, this time is
searching for a book’s author based on the input title.
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
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.
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;}
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(""); } }
Deleting Data
public void deleteBook(String title) { SQLiteDatabase db = this.getWritableDatabase();
db.delete(tblName, "title=?", new String[] { String.valueOf(title) });
db.close();
}
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(""); }
}
Activity Life Cycle
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().
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 }
}