Mad Unit 5 Imp Questions
Mad Unit 5 Imp Questions
SAQs
1. What is SQLite? Explain its features.
Ans)
Android provides structured data persistence through a combination of SQLite databases and
Content Providers. SQLite databases can be used to store application data using a managed,
structured approach. Android offers a full SQLite relational database library. Every application
can create its own databases over which it has complete control. SQLite is embedded into every
android device. Since the database requires limited memory at runtime (approx. 250KB) it is
widely chosen for embedding in devices.
SQLite is a well-regarded relational database management system RDBMS). It is:
1. Open-source
2. Standards-compliant
3. Lightweight
4. Single-tier
It has been implemented as a compact C library that’s included as part of the Android
software stack.
2. What are design consideration of SQLite Database
Ans)
while designing the database we need to keep the following Android-specific
considerations in mind your database.
1. Files (such as bitmaps or audio files) are not usually stored within database tables.
Use a string to store a path to the file, preferably a fully qualified URI.
2. Although not strictly a requirement, it’s strongly recommended that all tables include
an auto-increment key field as a unique index field for each row. If you plan to share
your table using a Content Provider, a unique ID field is required.
SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_NAME,
Context.MODE_PRIVATE, null);
After we have created the database, we must handle the creation and upgrade logic handled
within the onCreate and onUpgrade handlers of the SQLite Open Helper typically using the
database’s execSQL method to create and drop tables, as required.
7. What is query method in SQLite Library.
Ans)
To execute a query on a Database object, use the query method, passing in the following parameters:
1. An optional Boolean that specifies if the result set should contain only unique value
2. The name of the table to query.
3. A projection, as an array of strings, that lists the columns to include in the result set.
4. A where clause that defines the rows to be returned. we can include ? wildcards that will be
replaced by the values passed in through the selection argument parameter.
5. An array of selection argument strings that will replace the ? wildcards in the where clause.
6. A group by clause that defines how the resulting rows will be grouped.
7. A having clause that defines which row groups to include if you specified a group by
clause.
8. A string that describes the order of the returned rows.
9. A string that defines the maximum number of rows in the result set i.e limit
String table = "table2";
String[] columns = {"column1", "column3"};
String selection = "column3 =?";
String[] selectionArgs = {"apple"};
String groupBy = null;
String having = null;
String orderBy = "column3 DESC";
String limit = "10";
Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy,
limit);
1. What is SQLite database? How to create, delete and update rows the database
explain ?
Ans)
SQLiteDataBase is the base class for working with a SQLite database in android and provides
methods to open, query, update and close the database. In addition it provides the execSQL()
method, which allows to execute an SQL statement directly.
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns
data.
To perform the following database operations, SQLiteDatabase class provides various methods.
a) Insert operations:- for inserting values to the database, we need to save data into
contentvalues object. ContentValues allow defining key/value pairs. The key represents the
table column_name and the value represents the content corresponding to that column.
ContentValues can be used for inserts and updates of database entries.
General Form
SQLiteDatabase db=this.getWrtitableDatabase();
ContentValues values=new ContentValues();
Values.put(Coulumn1, Value1);
Values.put(Coulumn2, Value2);
Values.put(Coulumn3, Value3);
Values.put(Coulumn4, Value4);
b) Update Operation:- to update the existing record in your Android sqlite database table,
you need to execute the following method.
General Form
c) Delete operation:- similarly you can perform the delete operation on an Android SQLite
database table. It takes three arguments. The first is the table name, the second is the where
clause and th e third is the value corresponding to that where clause.
General Form
Db.delete(TABLE_NAME,WHERE_CLAUSE,WHERE_ARGUMENTS);
d) Read operation:-to read values from an Android SQLite database table, we must use
cursors. We execute the select operation on the database and we get multiple rows as a
result. We assign those to a cursor. A cursor points to one row at a time. This is how
we get the required data.
RawQuery: This query directly accepts SQL as an input. you can pass SQL statements(s)
and SQLiteDatabase will execute that query for you.
Public Cursor rawQuery (String sql, String[] selectionArgs)
Here authorities is the URI authority to access the Content provider. Typically this will be the
fully qualified name of the content provider class.