DataBase in Android
• SQLite is a opensource SQL database that
stores data to a text file on a device. Android
comes in with built in SQLite database
implementation.
• SQLite supports all the relational database
features. In order to access this database, you
don't need to establish any kind of
connections for it like JDBC,ODBC e.t.c
Database - Package
• The main package is android.database.sqlite
that contains the classes to manage your own
databases
Database - Creation
• In order to create a database you just need to
call this method openOrCreateDatabase with
your database name and mode as a
parameter. It returns an instance of SQLite
database which you have to receive in your
own object.Its syntax is given below
SQLiteDatabase mydatabase = openOrCreateDatabase("your
database name",MODE_PRIVATE,null);
Database - Insertion
• we can create table or insert data into table
using execSQL method defined in
SQLiteDatabase class. Its syntax is given
below:
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS
TutorialsPoint(Username VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO TutorialsPoint
VALUES('admin','admin');");
Database - Fetching
• We can retrieve anything from database using
an object of the Cursor class. We will call a
method of this class called rawQuery and it
will return a resultset with the cursor pointing
to the table. We can move the cursor forward
and retrieve the data.
Cursor resultSet = mydatbase.rawQuery("Select * from
TutorialsPoint",null);
resultSet.moveToFirst();
String username = resultSet.getString(0);
String password = resultSet.getString(1);
Database - Helper class
• For managing all the operations related to the
database , an helper class has been given and
is called SQLiteOpenHelper. It automatically
manages the creation and update of the
database.