Top Banner
ANDROID DATABASE TUTORIAL Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar Mr. Pritesh N. Patel 1
12

ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Apr 07, 2019

Download

Documents

tranthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

ANDROID DATABASE TUTORIAL

Mr. Pritesh N. PatelAssistant ProfessorMCAISTAR, V. V. Nagar

Mr. Pritesh N. Patel 1

Page 2: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Storage Options Android provides several options for you to save

persistent application data.

Your data storage options are the following:1. Shared Preferences : Store private primitive data in

key-value pairs.2. Internal Storage : Store private data on the device

memory.3. External Storage : Store public data on the shared

external storage.4. SQLite Databases : Store structured data in a private

database.5. Network Connection : Store data on the web with

your own network server.

Mr. Pritesh N. Patel 2

Page 3: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Using SharedPreferencs Preferences are typically name value pairs. They can be

stored as “Shared Preferences” across various activitiesin an application (note currently it cannot be sharedacross processes).

The context object lets you retrieveSharedPreferences through the methodContext.getSharedPreferences().

Mr. Pritesh N. Patel 3

CreationSharedPreferences myPrefs =

this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);

SharedPreferences.Editor prefsEditor =

myPrefs.edit();

prefsEditor.putString(“Name”, “Pritesh");

prefsEditor.putString(“Desig”, “Professor");

prefsEditor.commit();

RetrivalSharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);

String prefName = myPrefs.getString(“Name”, "nothing");String wallPaper = myPrefs.getString(“Desig”, null);

Page 4: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Introduction Android provides several ways to store user and app data.

SQLite is one way of storing user data. SQLite is a verylight weight database which comes with Android OS.

Here we will see how to create and manage database usingSQLite Database.

Things to consider when dealing with SQLite:

1. Data type integrity is not maintained in SQLite, you can put a value of a certain data type in a column of another datatype (put string in an integer and vice versa).

2. Referential integrity is not maintained in SQLite, there is no FOREIGN KEY constraints or JOIN statements.

3. SQLite Full Unicode support is optional and not installed by default.

Mr. Pritesh N. Patel 4

Page 5: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Database Structure

Field Name Type

empid Integer ( Primary Key )

empname Text

Database Name: demoDB

Table Name: tblemployees

Mr. Pritesh N. Patel 5

Page 6: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Creating Database and Tables

By default, SQLite on Android does not have amanagement interface or an application to createand manage databases from, so we're going tocreate the database ourselves by code.

static final String dbName="demoDB";

static final String employeeTable="Employees";

static final String colID="EmpId";

static final String colName="EmpName";

Mr. Pritesh N. Patel 6

Page 7: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Demo – Database and Table

private SQliteDatabase db;

db = openOrCreateDatabase(“demodb”, SQLiteDatabase.CREATE_IF_NECESSARY, null);

db.setLocale(Locale.getDefault());

db.setLockingEnabled(true);

db.setVersion(1);

db.execSQL(“CREATE TABLE employee(

empid INTEGER PRIMARY KEY, empname TEXT)”);

Mr. Pritesh N. Patel 7

Page 8: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Insert Operation

Syntax

insert(String tableName, String nullColumnHack, ContentValues values);

//Get Values from control

ContentValues cv = new ContentValues();

cv.put(“empid”,txteno.getText().toString());

cv.put(“empname”,txtenm.getText().toString());

long count = db.insert(“employee”, null, cv);

Mr. Pritesh N. Patel 8

Page 9: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Update Operation

Syntax

update(String tableName, ContentValues values, String whereClause, String[]whereArgs);

//Get Values from control

ContentValues cv = new ContentValues();

cv.put(“empname”,txtenm.getText().toString());

long count = db.update(“employee”, cv,”empid=?”, new String[] {txteno.getText().toString()} );

Mr. Pritesh N. Patel 9

Page 10: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Delete Operation

Syntax

delete(String tableName, String whereClause, String[] whereArgs);

//Get Values from control

long count = db.delete(“employee”,”empid=?”, new String[] {txteno.getText().toString()} );

Mr. Pritesh N. Patel 10

Page 11: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Select OperationSyntax

query(String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);

//Get Values from control

Cursor c = db.query(“employee”,null, null, null, null, null, null);

c.moveToFirst();

while(c.isAfterLast())

{Toast.makeText(getApplicationContext(), "*" + c.getString(0) + "|" + c.getString(1), Toast.LENGTH_SHORT).show();

c.moveToNext();}

Mr. Pritesh N. Patel 11

Page 12: ANDROID DATABASE TUTORIAL - Muaz Gultekin - Blog · Introduction Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very

Thank You

Mr. Pritesh N. Patel 12

[email protected]

+91 972-634-4355

http://priteshpatel.co.in