28
USING SQLITE Databases in Android Application

Databases in Android Application

Embed Size (px)

DESCRIPTION

SQLite Database in Android Application

Citation preview

Page 1: Databases in Android Application

USING SQLITEDatabases in Android Application

Page 2: Databases 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.

Page 3: Databases in Android Application

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

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

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

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

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

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

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

Creating Database in an Android App using SQLite

6. Add the constructor

Page 11: Databases in Android Application

Creating Database in an Android App using SQLite7. Add these two API methods:

Page 12: Databases in Android Application

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

Adding a Record to the Database2. Add the addBook method, with this you

need to import ContentValues

Page 14: Databases in Android Application

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

Implementing Database1. On the MainActivity, add a method for

getting user input and adding it to the database

Page 16: Databases in Android Application

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

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

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

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

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

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

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

Activity Life Cycle

Page 24: Databases in Android Application

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

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
Page 27: Databases in Android Application
Page 28: Databases in Android Application

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    }

}