Session06 SQLite
Session06 SQLite
Session06 SQLite
Session 6
Objective
What is SQLite?
SQLite Datatype
Database – Creation
Database - Helper class
Select, Insert, Update, Delete in SQLite
What is SQLite
http://www.sqlite.org/datatype3.html
android.database.sqlite
-Contains the SQLite database
management classes that an application
would use to manage its own private
database.
- Create a database you just need to call
this method openOrCreateDatabase with
your database name and mode as a
parameter.
android.database.sqlite - Classes
import android.database.sqlite.SQLiteDatabase;
SQLiteDatabase myDatabase;
Where the assumed prefix for the database stored in the devices RAM is:
"/data/data/<CURRENT_namespace>/databases/". For instance if this
app is created in a namespace called “cis493.sql1”, the full name of the
newly created database will be:
“/data/data/cis493.sql1/databases/myfriendsDB”.
Create a SQLite database
SQL Syntax for the creating and populating of a table looks like this:
Create a static string containing the SQLite CREATE statement, use the execSQL(
) method to execute it.
myDatabase.execSQL(createAuthor);
Creating Tables
The table has three fields: a numeric unique identifier called recID, and two string fields
representing our friend’s name and phone.
If a table with such a name exists it is first dropped and then created anew. Finally three rows
are inserted in
the table.
Insert
Parameters
table the table to delete from
whereClause the optional WHERE clause to apply when deleting.
Passing null will delete all rows.
Returns the number of rows affected if a whereClause is passed in,
0 otherwise.
To remove all rows and get a count pass "1" as the whereClause.
Delete
Consider the following SQL statement:
After the substitutions are made the resulting SQL statement is:
Queries
Simple Queries
The signature of the Android’s simple query method is:
Queries
Query the EmployeeTable, find the average salary of female
employees supervised by 123456789. Report results by Dno. List
first the highest average, and so on, do not include depts. having
less than two employees.
Queries
The following query selects from each row of the tblAMIGO table
the columns: recID, name, and phone. RecID must be greather
than 2, and names must begin with ‘B’ and have three or more
letters.
Queries
An Android solution for the problem using a simple template query
follows.
Queries
Observations
1. The selectColumns array indicates two fields name which is already part of the table, and
TotalSubGroup which is to be computed as the count(*) of each name sub-group.
2. The symbol ? in the whereCondition is a place-marker for a substitution. The value “3” taken from the
whereConditionArgs is to be injected there.
3. The groupBy clause uses ‘name’ as a key to create sub-groups of rows with the same name value. The
having clause makes sure we only choose subgroups no larger than four people.
Cursors
Android cursors are used to gain (sequential & random) access to tables produced by SQL select statements.
Cursors primarily provide one row-at-the-time operations on a table.
Cursors include several types of operators, among them:
1. Positional awareness operators (isFirst(), isLast(), isBeforeFirst(),
isAfterLast() ),
2. Record Navigation (moveToFirst(), moveToLast(), moveToNext(),
moveToPrevious(), move(n) )
3. Field extraction (getInt, getString, getFloat, getBlob, getDate, etc.)
4. Schema inspection (getColumnName, getColumnNames, getColumnIndex, getColumnCount, getCount)
Cursors
Conclude
Using SQLite you can make Notes app, saving marker news, saving high
score in game, expenditure management app, …etc.
Summary