Please enable JavaScript.
Coggle requires JavaScript to display documents.
Databases and ContentProviders (SQLite) (Classes (SQLiteCloseable-An…
Databases and ContentProviders (SQLite)
SQLite Database
is a
small C library that implements a self-contained, embeddable, zero-configuration SQL database engine.
open source embedded database
Lightweight-many applications and Internet browsers use SQLite due to its very small size.
SQLite uses a relaxed data typing model.
Instead of assigning a type to an entire column, types are assigned to individual values.
Major Users
Adobe(photo)
Apple (mail&safari)
Mozilla(firefox store metadata)
Google-in google desktop
McAfee(in Anti-virus programs)
Phillips-mp3 players(store metadata)
PHP
Python-bundled with the Python programming language.
Unique Features
No configuration
No server process to administer or user accounts to manage
Easy to backup and transmit database (just copy the file)
Dynamic typing for column values, variable lengths for column records
Consortium
is a membership association dedicated to the development of SQLite
goals
are to keep SQLite both high quality and public domain
Key members
include Adobe, Bloomberg, Mozilla and Symbian.
Classes
SQLiteCloseable
-An object created from a SQLiteDatabase that can be closed.
SQLiteCursor
-A Cursor implementation that exposes results from a query on a SQLiteDatabase
SQLiteDatabase
- to manage a SQLite database
SQLiteOpenHelper
-A helper class to manage database creation and version management.
SQLiteProgram
-A base class for compiled SQLite programs.
SQLiteQuery
-A SQLite program that represents a query that reads the resulting rows into a CursorWindow.
SQLiteQueryBuilder
-helps to build SQL queries to be sent to SQLiteDatabase objects.
SQLiteStatement
-A pre-compiled statement against a SQLiteDatabase that can be reused.
ways
SQLiteOpenHelper class
A helper class to manage database creation and version management.
steps
1- call the super constructor and send
(context, db name, factory, version)
2-
import
android.database.
sqlite
3- Extend the
SQLiteOpenHelper
4- override method
onCreate() :
to create the database
5- it receive SQLiteDatabase db
(CREATE TABLES HERE)
execSQL(create_table)
onUpgrade()
:
to update the schema any time it is
modified
syntax
: SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactoryfactory, intversion)
METHOD
getWriteableDatabase() :
to open and obtain a writable instance of the underlying database,
getReadableDatabase()
:to open and obtain a read-only instance of the underlying database
getWritableDatabase fail==>
disk space or permission issues,
==> fall back to the getReadableDatabase
for database queries if necessary
.
ContentValues
represent as
ContentValues object
represent a single table row as a map of Colum names to values.
Key
is the Column and
Value
is the selected key's value
Insert
need
table name, ullColumnHack and the content value
if true
return row ID
else return -1
nullColumnHack
parameter provides the name of nullablecolumn name
should follow by
this.connect();
update
update(
tablename
,
content value
,
whereClause
,
whereArgs
)
delete
delete(
tablename
,
whereClause
,
whereArgs
);
CURSORS
Database queries
are returned as Cursor objects.
used to
gain (sequential & random) access to tables produced by SQL select statements.
pointers to the result set
provide
one row-
at-the-time operations on a table.
types of operators
Positional awareness operators
isFirst()
isLast()
Record Navigation
moveToLast()
moveToNext()
Field extraction
getString,
getDate,
Schema inspection
getColumnName,
getColumnIndex,
Queries
Method of
SQLiteDatabase
class and performs queries on the DB and returns the
results in a Cursor object
Cursor c = mdb.query(p1,p2,p3,p4,p5,p6,p7)
p1 ; Table name (String)
p2 ; Columns to return (String array)
p3 ; WHERE clause (use null for all, ?s for selection args)
p4 ; selection argvalues for ?s of WHERE clause
p5 ; GROUP BY ( null for none) (String)
p6 ; HAVING (null unless GROUP BY requires one) (String)
p7 ; ORDER BY (null for default ordering)(String)
p8 ; LIMIT (null for no limit) (String)
Without SQLiteOpenHelper class
1- Create a database:
openOrCreateDatabase
((dbName,Context.MODE_PRIVATE,null)
in part "1"
MODE
coul be
MODE_PRIVATE
:the
default
mode, only be accessed by the calling application(user ID)
MODE_WORLD_READABLE:
other applications to have read access
MODE_WORLD_WRITEABLE:
other applications to have write access
Cursor
execSQL(): Execute a single SQL statement that is
NOT a SELECT
or any other SQL statement that
not returns data
rawQuery(): executes an SQL query statement and
returns matching results
in the form of a
Cursor object.
Some method
requestFocus();
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+editRollno.getText()+"'", null); عشان ارجع الرو المطلوب
c.moveToFirst() راح اتاكد ان موجود
c.getString(1) ترجع لي كولم محدد من الرو اللي اني اخترته
StringBuffer()
builder.setCancelable(true);
advantage of Database
Constants
Useful for query definition
useful in the Content providers
ماراح احتاج اني اكتبهم ف كل مرة وأخطأ ف كتابتهم ، خلاص بصير استخدم بس المتغيرات اللي حفظت فيهم اسماء الكولمز
The index (key) column name
for use
in where clauses.
public static final String KEY_ID = “_id”;
steps to create DB
1- define the schema (
db name, version, table name, column names)
2- create database
(write query to create/modify db)
3- execute the query
(perform insert/update/delete)
Context
Provides global information about an application environment
represents environment data
of current state
is the base class for Activity,Service,Application
thing involve context
Loading a resource
Launching a new activity.
Creating views.
obtaining system service
method
getApplicationContext()
:returns the application context of the entire application life cycle,when application will destroy then it will destroy also.
getContext()
getBaseContext()
: Proxying implementation of Context so indirectly we are passing a Context Class Object اطبقه بشكل غير مباشر باستخدام الاوبجكت تبع الكونتكست
this:
returns the current context of the activity
provides
Create a database
.
Create a table
Insert values into table
Retrieve values
Update values
Delete values
execSQL():
which allows to execute an SQL statement directly
diff