sqlite
sqlite
▪Though SQLite offers quite an impressive feature set given its size, it
differs in many aspects from a conventional database system:
▪SQLite is serverless
▪SQLite stores data in one database file
▪SQLite offers only a few data types
▪SQLite uses manifest typing instead of static types
▪SQLite has no fixed column length
▪SQLite uses cross-platform database files
SQLite data types
SQLite data types
▪SQLite offers fewer datatypes
▪If you use other types (like varchar) in your CREATE TABLE statement SQLite
maps them as closely as possible to any of these types.
▪Most SQL database uses static, rigid typing. With static typing, the datatype of a
value is determined by its container - the particular column in which the value is
stored.
▪SQLite uses a more general dynamic type system.
▪ In SQLite, the datatype of a value is associated with the value itself, not with its
container.
▪SQLite does not have a separate Boolean storage class. Instead, Boolean values are
stored as integers 0 (false) and 1 (true).
SQLite data types
▪SQLite does not have a storage class set aside for storing dates and/or times. Instead,
the built-in Date And Time Functions of SQLite are capable of storing dates and
times as TEXT, REAL, or INTEGER values:
▪BLOB stands for a binary large object that is a collection of binary data stored as a
value in the database. By using the BLOB, you can store the documents, images, and
other multimedia files in the database
Database manipulation using SQLLite
▪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.
Database manipulation using SQLLite
▪Database - Creation
▪Syntax
▪SQLiteDatabase mydatabase = openOrCreateDatabase("your database
name",MODE_PRIVATE,null);
▪Table creation
▪mydatabase.execSQL("CREATE TABLE IF NOT EXISTS testtable(Username
text, Password text ) ;");
Database manipulation
▪To perform insert, read, delete, update operation there are two different ways:
▪Write parameterized queries (Recommended)
▪Write raw queries
▪Parameterized Queries: These are those queries which are performed using
inbuilt functions to insert, read, delete or update data. These operation related
functions are provided in SQLiteDatabase class.
▪Raw Queries: These are simple sql queries similar to other databases like
MySql, Sql Server etc , In this case user will have to write query as text and
passed the query string in rawQuery(String sql ,String [] selectionArgs) or
execSQL(String sql,Object [] bindArgs) method to perform operations.
Example of raw query to insert data:
◦ mydatabase.execSQL("INSERT INTO testtable VALUES('admin','admin');");
1. SQLite Database: This is the Android’s Java interface to its relational database,
SQLite. It also supports an SQL or Structured Query Language implementation which is
rich enough to handle any requirement for mobile application.
2.Cursor: A cursor is a container which holds the results of a database query and also
supports the commonly used MVC design pattern.
Cursors are very much similar to the JDBC result sets which returns the value of any
database query. A cursor also has the ability to represent multiple objects without
creating instance for each and every one.
The cursor returns the values depending upon the current position of the cursor index,
which we can increase by calling the method Cursor.moveToNext, or decrease by
calling Cursor.moveToPrevious as we need
Android Database Classes
3. SQLiteOpenHelper: The open helper provides a life cycle framework to
create and upgrade our application database. This class is used to provide
assistance in the critical task which transitions the data from one version of an
application to a new set of database tables in a newer version of same or another
application.
4. SQLiteQueryBuilder: The query builder provides a high-level abstraction to
create SQLite queries to be used in Android applications. In fact we can simplify
the task of writing queries for our application and it also saves an enormous
amount of time.