O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

android sqlite

5.187 visualizações

Publicada em

Publicada em: Educação, Tecnologia
  • Login to see the comments

android sqlite

  1. 1. Let us work with Sqlite Table 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
  2. 2. 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.
  3. 3. 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.
  4. 4. 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)
  5. 5. 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.
  6. 6. 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]
  7. 7. Architecture of Sqlite
  8. 8. 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
  9. 9. Architecture of Sqlite When 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
  10. 10. 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.
  11. 11. 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
  12. 12. Examples To Do Let us do some coding Steps in working with Sqlite database:  Creating Sqlite Object  Creating Database  Creating Table  Working with Tables
  13. 13. Examples To Do Let us do some coding Step:1 Importing package “android.database.sqlite.SQLiteDatabase”. Step:2 Creating object SQLiteDatabase object name=null;
  14. 14. Examples To Do Let us do some coding 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
  15. 15. Examples To Do Let us do some coding mydb.execSQL("CREATE TABLE IF NOT EXISTS “ +TableName+" (ColumnName DataType);");
  16. 16. Examples To Do Let us do some coding Create: mydb.execSQL("CREATE TABLE IF NOT EXISTS “ +TableName+" (ColumnName DataType);"); Alter: ALTER TABLE TableName RENAME TO new-table-name Drop: DROP TABLE TableName (View Source)
  17. 17. Examples To Do Let us do some coding 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)
  18. 18. 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)”.
  19. 19. Examples To Do The 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.
  20. 20. Examples To Do The 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.
  21. 21. Examples To Do The 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.