22
ANDROID SQLITE

android sqlite

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: android sqlite

ANDROID SQLITE

Page 2: android sqlite

Let us work with SqliteTable of contents

SNO Name Of Content

1 History Of SQLite Database

2 Why SQLite Only???

3 Features Of SQLite

4 Disadvantages Of SQLite

5 Architecture Of SQLite

6 Examples To Do

Page 3: android sqlite

History•SQlite is an open source embedded database. The original

implementation was designed by D. Richard Hipp.

•Hipp was designing software used on board guided missile systems and thus had limited resources to work with.

•The resulting design goals of SQLite were to allow the program to be operated without a database installation or administration.

Page 4: android sqlite

Why Sqlite Only????

• In android we will use Sqlite database only . Because it is in built DB in Android SDK more over it is lite weighted relation DB suitable for Mobile Devices.

•We need not to load any drivers and we need not to install the Sqlite separately.

•The queries also simple to understand and easy to implement.

Page 5: android sqlite

Feature of SQLite

•Application file format – Transactions guarantee ACID [Atomicity, Consistency , Isolation, Durability] even after system crashes and power failures.

•Temporary data analysis – Command line client, import CSV files and use sql to analyze & generate reports .

•Embedded devices – Applicable to small, reliable and portable like mobiles.

•Portable - uses only ANSI-standard C and VFS, file format is cross platform (little vs. big endian, 32 vs. 64 bit)

Page 6: android sqlite

Feature of SQLite

•Reliable – has 100% test coverage, open source code and bug database, transactions are ACID even if power fails.

•Small – 300 kb library, runs in 16kb stack and 100kb heap.

•Single Database File – An SQLite database is a single ordinary disk file that can be located anywhere in the directory hierarchy.

•Readable source code – The source code to SQLite is designed to be readable and accessible to the average programmer.

Page 7: android sqlite

Disadvantages

•High concurrency – reader/writer locks on the entire file.

•Huge datasets – DB file can’t exceed file system limit or 2TB.

•Access control – we don’t have any user interface to operate Sqlite database objects as in MYSQL / SQL Server /Oracle. All the objects are virtual. However there are fewq third party UI are available in the market. [http://www.sqlabs.net/sqlitemanager.php]

Page 8: android sqlite

Architecture of Sqlite

Page 9: android sqlite

Architecture of Sqlite

The SQL Command Processor will read the SQL Commands from Interface and it will forward that SQL Query Strings to the Tokenizer of SQL Compiler

This Virtual Machine will read the code what ever the code generated by the code generator of the SQL Compiler

This interface will read the SQL queries what ever it is generated by the user and it will pass this SQL Commands to the SQL Command Processor

Page 10: android sqlite

Architecture of SqliteWhen a string containing SQL statements is to be executed, the interface passes that string to the tokenizer. The job of the tokenizer is to break the original string up into tokens and pass those tokens one by one to the parser. The tokenizer is hand-coded in C in the file tokenize.c.

The parser is the piece that assigns meaning to tokens based on their context, it does not leak memory when syntax errors are

encountered, parser assembles tokens into complete SQL statements

the code generator to produce virtual machine code that will do the work that the SQL statements request. Ex: vacuum.c and where.c in which where.c handles code generation

for WHERE clauses on SELECT, UPDATE and DELETE statements

Page 11: android sqlite

Architecture of Sqlite

The B-tree module requests information from the disk in fixed-size chunks. The default chunk size is 1024 bytes but can vary

between 512 and 65536 bytes. The page cache is responsible for reading, writing, and caching these chunks. The page cache also provides the rollback and atomic commit abstraction and takes

care of locking of the database file.

In order to provide portability between POSIX (Portable Operating System Interface (for Unix))and Win32 operating systems, SQLite uses an abstraction layer to interface with the

operating system. The interface to the OS abstraction layer is defined in os.h.

An SQLite database is maintained on disk using a B-tree implementation found in the btree.c source file. A separate B-tree

is used for each table and index in the database. All B-trees are stored in the same disk file. Details of the file format are recorded

in a large comment at the beginning of btree.c.

Page 12: android sqlite

Architecture of Sqlite

Memory allocation and ceaseless string comparison routines are located in util.c

If you count regression test scripts, more than half the total code base of SQLite is devoted to testing. There are many assert()

statements in the main code files

Page 13: android sqlite

Examples To DoLet us do some coding

Steps in working with Sqlite database: Creating Sqlite Object Creating Database Creating Table Working with Tables

Page 14: android sqlite

Examples To DoLet us do some coding

Creating Sqlite Object

Step:1 Importing package “android.database.sqlite.SQLiteDatabase”.Step:2 Creating objectSQLiteDatabase object name=null;

Page 15: android sqlite

Examples To DoLet us do some coding

Creating Database Name

mydb=openOrCreateDatabase("DatabaseName5", MODE_PRIVATE,null);

EX:

//mydb is sqlite object name .//DatabaseName5 is nothing but database name //MODE_PRIVATE is permissions of a table accessing

Page 16: android sqlite

Examples To DoLet us do some coding

Creating Table

mydb.execSQL("CREATE TABLE IF NOT EXISTS “ +TableName+" (ColumnName DataType);");

Page 17: android sqlite

Examples To DoLet us do some coding

DDL(Create ,Alter ,Drop)

Create:mydb.execSQL("CREATE TABLE IF NOT EXISTS “

+TableName+" (ColumnName DataType);");Alter:

ALTER TABLE TableName RENAME TO new-table-nameDrop:

DROP TABLE TableName (View Source)

Page 18: android sqlite

Examples To DoLet us do some coding

DML(SELECT , INSERT ,

DELETE)Select:

Cursor c=mydb.rawQuery("SELECT * FROM "+TableName+" where Name='"+city+"'",null);Insert:

mydb.execSQL("INSERT INTO "+TableName+“ (Name, Area)“ + "VALUES ('RedFort','40.8 acres‘);");

Delete: mydb.execSQL(“Delete"+TableName);

(View Source)

Page 19: android sqlite

SQLite OpenHelper•SQLite OpenHelper is a class to manage database creation

and version management.

•This class take care of opening the database if it exists, creating it if it does not, and upgrading it as necessary.

• This is for creating db “onCreate(SQLiteDataBase)”.

• when the database needs to be upgraded “onUpgrade (SQLiteDataBase db, int oldVersion, int newVersion)”.

• when the database has been opened “onOpen (SQLiteDataBase db)”.

Page 20: android sqlite

Examples To DoThe below are few kind of applications that we can develop using Sqlite in Android.

Ex:1. Find the registration form in a Android application given here. Functional Description: Once the user enters all the details given and click on Submit Button we need to SAVE the entire data in Sqllite using queries.

Page 21: android sqlite

Examples To DoThe below are few kind of applications that we can develop using Sqlite in Android.

Ex:2. Find the List View in a Android application given here. Functional Description: Once the user Stores The data ,You have to read that data and show In list view. Example if we have all country names in the Database then we can show as here.

Page 22: android sqlite

Examples To DoThe below are few kind of applications that we can develop using Sqlite in Android.

Ex:3. Find the Tab View in a Android application given here. Functional Description: Once the user Stores The data ,You have to read that data and show In Tab View.