Working with Files and Database 1 SQLite Database
Working with Files and Database 1
SQLite Database • Is an open source database • Supports standard relational database features • Android comes in with built in SQLite database implementation • To create and upgrade a database in your android application you create a subclass of the SQLite. Open. Helper class. • In the constructor of your subclass you call the super() method of SQLite. Open. Helper 2
• SQLite used to perform database operations on android devices such as – storing, – manipulating or retrieving persistent data from the database. • SQLite. Open. Helper class provides the functionality to use the SQLite database. 3
SQLite. Open. Helper class • used for database creation and version management. • For performing any database operation, you have to provide the implementation of on. Create() and on. Upgrade() methods of SQLite. Open. Helper class. 4
Constructors of SQLite. Open. Helper class • creates an object for creating, opening and managing the database. 5
Methods of SQLite. Open. Helper class • There are many methods in SQLite. Open. Helper class. Some of them are as follows: Method Description public abstract void on. Create(SQLite. Database db) called only once when database is created for the first time. public abstract void on. Upgrade(SQLite. Database db, int old. Version, int new. Version) called when database needs to be upgraded. public synchronized void close () closes the database object. public void on. Downgrade(SQLite. Database db, int old. Version, int new. Version) called when database needs to be downgraded. 6
SQLite. Database class • It contains methods to be performed on sqlite database such as create, update, delete, select Method Description etc. void exec. SQL(String sql) executes the sql query not select query. long insert(String table, String null. Column. Hack, Content. Values values) inserts a record on the database. The table specifies the table name, null. Column. Hack doesn't allow completely null values. If second argument is null, android will store null values if values are empty. The third argument specifies the values to be stored. int update(String table, Content. Values values, String where. Clause, String[] where. Args) updates a row. Cursor query(String table, String[] columns, String selection, String[] selection. Args, String group. By, String having, String order. By) returns a cursor over the resultset. 7
Steps to create SQLite Database • • Create a new java class called=Database. Helper Extend it with SQLite. Open. Helper Implement all the methods Example table: student ID Name Mark 1 Ahmed 75 2 Lili 88 3 Beti 65 • Now lets create the database inside the constructor public Database. Helper( Context context ){ Super(context, database. Name, null, 1); } 8
Table • To create a table: use on. Create(…. ) method public void on. Create(SQLite. Database sq. Lite. Database) { } • To drop a table: use on. Upgrade(……. ) method public void on. Upgrade(SQLite. Database sq. Lite. Database, int i 1) { } 9
Content. Values • Content. Values is a name value pair, • used to insert or update values into database tables. • Content. Values object will be passed to SQLite. Data. Base objects insert() and update() functions. • Cursor is a temporary buffer area which stores results from a SQLite. Data. Base query. 10
Exercise 11
Thank You ? 12
- Slides: 12