Android ch-8
Android ch-8
Android ch-8
Shared preferences are good for storing ... an application's preferences, and other small
bits of data. It's a just really simple persistent string key store for a few data types:
boolean, float, int, long and string. So for instance if my app had a login, I might consider
storing the session key as string within Shared Preferences.
Internal storage is good for storing application data that the user doesn't need access to,
because the user cannot easily access internal storage. Possibly good for caching, logs,
other things. Anything that only the app intends to Create Read Update or Delete.
External storage. Great for the opposite of what I just said. The dropbox app probably
uses external storage to store the user's dropbox folder, so that the user has easy access to
these files outside the dropbox application, for instance, using the file manager.
SQLite databases are great whenever you are going to use a lot of structured data and a
relatively rigid schema for managing it. Put in layman's terms, SQLite is like MySQL or
PostgreSQL except instead of the database acting as a server daemon which then takes
queries from the CGI scripts like php, it is simply stored in a .db file, and accessed and
queried through a simple library within the application. While SQLite cannot scale nearly
as big as the dedicated databases, it is very quick and convenient for smaller applications,
like Android apps. I would use an SQLite db if I were making an app for aggregating and
downloading recipes, since that kind of data is relatively structured and a database would
allow for it to scale well. Databases are nice because writing all of your data to a file,
then parsing it back in your own proprietary format it no fun. Then again, storing data in
XML or JSON wouldn't be so bad.
Network connection refers to storing data on the cloud. HTTP or FTP file and content
transfers through the java.net.* packages makes this happen.
SQLITE
SQLite is a freely available open source database provided in Android. SQLite is a lightweight
and compact database that does not require any kind of server to run. It is easily integrated into
any kind of mobile application. There are many libraries and classes available on Android to
perform any kind of database queue on SQLite. It provides so many commands like add new
data, update, read, and delete data.
Features of SQLite
1. The transactions follow ACID properties i.e. atomicity, consistency, isolation, and
durability even after system crashes and power failures.
2. The configuration process is very easy, no setup or administration is needed.
3. All the features of SQL are implemented in it with some additional features like partial
indexes, indexes on expressions, JSON, and common table expressions.
4. Sometimes it is faster than the direct file system I/O.
5. It supports terabyte-sized databases and gigabyte-sized strings and blobs.
6. Almost all OS supports SQLite like Android, BSD, iOS, Linux, Mac, Solaris, VxWorks,
and Windows (Win32, WinCE, etc. It is very much easy to port to other systems.
7. A complete database can be stored in a single cross-platform disk file.
Applications of SQLite
1. Due to its small code print and efficient usage of memory, it is the popular choice for
the database engine in cell phones, PDAs, MP3 players, set-top boxes, and other
electronic gadgets.
2. It is used as an alternative for open to writing XML, JSON, CSV, or some proprietary
format into disk files used by the application.
3. As it has no complication for configuration and easily stores file in an ordinary disk
file, so it can be used as a database for small to medium sized websites.
4. It is faster and accessible through a wide variety of third-party tools, so it has great
applications in different software platforms.
SQLite Disadvantages
1. SQLite is used to handle low to medium traffic HTTP requests.
2. Database size is restricted to 2GB in most cases.
SQLite Commands
In SQLite,DDL (Data Definition Language) is used to create and modify database objects such
as tables, indices, and views. Some examples of DDL statements in SQLite are:
CREATE TABLE: creates a new table in the database
ALTER TABLE: modifies an existing table in the database
DROP TABLE: deletes a table from the database
CREATE INDEX: creates a new index on a table
DROP INDEX: deletes an index from a table
DML (Data Modification Language) is used to modify the data stored in the database. Some
examples of DML statements in SQLite are:
INSERT INTO: inserts a new row into a table
UPDATE: updates the data in one or more rows of a table
DELETE FROM: deletes one or more rows from a table
DQL (Data Query Language) is used to retrieve data from the database. Some examples of
DQL statements in SQLite are:
SELECT: retrieves data from one or more tables in the database
JOIN: retrieves data from multiple tables based on a common field
GROUP BY: groups the results of a query by one or more fields
HAVING: filters the results of a query based on a condition
3. SQLiteOpenHelper is a class that is useful to create the database and manage it. The two
constructors of SQLiteOpenHelper class are:
SQLiteOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version_no) – It creates objects for
creating, opening, and managing the database.
SQLiteOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version_no,
DatabaseErrorHandler errorHandler) – Its object specifies the error handler
along with creating, opening, and managing the database.
4. SQLiteDatabase is a class that has methods to perform operations such as create, update,
delete, etc.
Cursors
Cursors are what contain the result set of a query made against a database in Android.
The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that
were returned from the query as well as iterate over the rows of the result set.
The Cursor class provides the following methods to manipulate its internal position:
1. boolean Cursor.move(int offset): Moves the position by the given offset
2. boolean Cursor.moveToFirst(): Moves the position to the first row
3. boolean Cursor.moveToLast(): Moves the position to the last row
4. boolean Cursor.moveToNext(): Moves the cursor to the next row relative to the current
position
5. boolean Cursor.moveToPosition(int position): Moves the cursor to the specified position
6. Cursor.moveToPrevious(): Moves the cursor to the previous row relative to the current
position
Content Providers
A content provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. A content provider can use
different ways to store its data and the data can be stored in a database, in files, or even over a
network.
Content providers let you centralize content in one place and have many different applications
access it as needed. A content provider behaves very much like a database where you can query
it, edit its content, as well as add or delete content using insert(), update(), delete(), and query()
methods. In most cases this data is stored in an SQlite database.
A content provider is implemented as a subclass of ContentProvider class and must implement a
standard set of APIs that enable other applications to perform transactions.
ContentProvider Methods
1. onCreate() This method is called when the provider is started.
2. query() This method receives a request from a client. The result is returned as a Cursor
object.
3. insert()This method inserts a new record into the content provider.
4. delete() This method deletes an existing record from the content provider.
5. update() This method updates an existing record from the content provider.
6. getType() This method returns the MIME type of the data at the given URI.
Content URIs
To query a content provider, you specify the query string in the form of a URI which has
following format − <prefix>://<authority>/<data_type>/<id>