Android UNIT 4
Android UNIT 4
SQLiteOpenHelper Class
SQLiteOpenHelper is an in-built class of android.database.sqlite.SQLiteDatabase package. It is a helper class to
manage SQLite database creation and version management. The helper class manages the creation of the
database, handling database manipulations, and also the version management. We need to create a subclass
extending from SQLiteOpenHelper class for database manipulations. In this class, we will implement two
overridden methods onCreate( ) and onUpgrade( ). These classes take care of opening the database if it exists,
creating it if it does not, and upgrading it as necessary.
onCreate( ) method
The onCreate( ) is called when the database is created for the first time. It is called only once throughout the
entire application lifecycle. It will be called whenever there is a first call to getReadableDatabase( ) or
getWritableDatabase( ) function. These functions are available in the super SQLiteOpenHelper class.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE my_table (id INTEGER PRIMARY KEY, name TEXT)");
}
onUpgrade( ) method
The onUpgrade( ) method is called when the database needs to be upgraded. It is called when the database file
already exists, and we want to upgrade its version.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS my_table");
onCreate(db);
}
Constructors of SQLiteOpenHelper class
Constructor Description
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int creates an object for creating, opening and managing the
version) database.
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int creates an object for creating, opening and managing the
version, DatabaseErrorHandler errorHandler) database. It specifies the error handler.
SQLiteDatabase class
It contains methods to be performed on sqlite database such as create, update, delete, select etc.
7. They enable us to make the functionalities of current programmes available to the public via the internet.
3. Web services utilise the HTTP protocol, which is unreliable and unsafe.
What is json?
JSON stands for JavaScript object notation. JSON has been derived from javascript, where javascript is a programming language.
It was originally created to hold the structured data that could be used in javascript. JSON became so popular that it is used for data
for all kinds of applications. It is the most popular way of sending the data for Web APIs.
What is XML?
XML stands for an extensible markup language. It is like HTML, where HTML stands for Hypertext Markup language. HTML is used
for creating websites, whereas XML can be used for any kind of structured data.
XML has two ways of handling data, i.e., Tags and Attributes. The tags work as HTML. The start tags start with the <_> and end with
the </_>. The start and end tags must match. The names must only be letters, numbers, and underscore, and the tag name must
start with a letter only.
The following are the differences between the json and xml:
JSON XML
JSON stands for javascript object notation. XML stands for an extensible markup language.
The extension of json file is .json. The extension of xml file is .xml.
The internet media type is application/json. The internet media type is application/xml or text/xml.
The type of format in JSON is data interchange. The type of format in XML is a markup language.
It is open source means that we do not have to pay anything to use JSON. It is also open source.
The object created in JSON has some type. XML data does not have any type.
The data types supported by JSON are strings, numbers, Booleans, null, array. XML data is in a string format.
It does not have any capacity to display the data. XML is a markup language, so it has the capacity to display the content.
JSON has no tags. XML data is represented in tags, i.e., start tag and end tag.
Security:
Security refers to the practice of protecting computer systems, data, and software from unauthorized access, damage, or theft, and
ensuring the confidentiality, integrity, and availability of information. In the context of Android programming, security encompasses
several aspects:
1. Data Security: Ensuring that sensitive data, such as user credentials, financial information, and personal data, is stored and
transmitted in a secure manner. This often involves encryption and secure protocols.
2. User Authentication: Verifying the identity of users to prevent unauthorized access to app features or data. Common methods
include password-based authentication, biometrics (e.g., fingerprint or face recognition), and OAuth.
3. Authorization: Controlling what actions or data a user or application is allowed to access based on their permissions or roles. This
is often managed through permissions and access control lists (ACLs).
4. Code Security: Protecting the integrity of your app's code and resources. This includes code obfuscation, avoiding code injection
vulnerabilities, and using secure coding practices.
5. Network Security: Ensuring that data transmitted over networks is secure. This includes using HTTPS for web communication and
implementing network security configurations.
6. Permissions: Requesting and handling permissions in a way that respects user privacy and security. Only request the permissions
your app truly needs, and handle them properly.
7. Vulnerability Testing: Regularly assessing your app for security vulnerabilities, such as SQL injection, cross-site scripting (XSS), and
data leakage.
Database Permissions:
In the context of Android programming and databases, "database permissions" refer to the rights and privileges assigned to users
or applications regarding database access. These permissions dictate what operations (e.g., read, write, delete) can be performed on
the database and its tables. Key points regarding database permissions include:
1. Least Privilege: Following the principle of least privilege, which means granting the minimum level of permissions necessary to
perform a task. This helps limit the potential impact of security breaches.
2. SQL Databases: In SQL databases (e.g., SQLite), permissions can be assigned to users or roles for tables, views, and other database
objects. Common permissions include SELECT, INSERT, UPDATE, DELETE, and EXECUTE.
3. Content Providers: In Android, when using Content Providers to share data between apps, permissions are specified to control
which apps can access the data and what operations they can perform.
4. Access Control: Implementing access control mechanisms to enforce permissions, ensuring that only authorized users or
components can interact with the database.
5. User Authentication: Verifying the identity of users or apps before granting access to the database. Authentication methods
include username/password, API keys, and tokens.
6. Database Connection Security: Securing the communication between your Android app and the database server, such as using
encrypted connections and ensuring the database server is properly configured.
7. Auditing and Logging: Implementing auditing and logging mechanisms to track database access and detect potential security
breaches or unauthorized activities.