0% found this document useful (0 votes)
2 views

Lecture 13 File Handling and SQLite DBMS

Uploaded by

sohailhamna50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 13 File Handling and SQLite DBMS

Uploaded by

sohailhamna50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Files and storage

Files and storage


Android provides many kinds of storage for applications to store their data.
These storage places are internal and external storage, SQLite storage, and
storage via network connection.

Internal and external storage.


– Both are persistent storage; data remains after power-off / reboot.
Internal storage is the storage of the private data on the device memory.
By default these files are private and are accessed by only your application
and get deleted , when user delete your application.

– typically smaller (~1-4 gb)


–specific and private to each app
– wiped out when the app is uninstalled
Using internal storage

● An activity has methods you can call to read/write files:


– openFileInput("name", mode) - opens a file for reading
– openFileOutput("name", mode) - opens a file for writing

● You can use standard classes like Scanner, BufferedReader, and


PrintStream to read/write files (see Java API)
Internal storage example 2
// write a short text file to the internal storage
PrintStream output = new PrintStream(
openFileOutput("out.txt", MODE_PRIVATE));

output.println("Hello, world!");
output.println("How are you?");
output.close();
...

// read the same file, and put its contents into a TextView
Scanner scan = new Scanner(
openFileInput("out.txt", MODE_PRIVATE));
String allText = ""; // read entire file
while (scan.hasNextLine()) {
String line = scan.nextLine();
allText += line;
}

myTextView.setText(allText);
scan.close();
Internal storage example 2

// read a file, and put its contents into a TextView


// (assumes hello.txt file exists in res/raw/ directory)
Scanner scan = new Scanner(
getResources().openRawResource(R.raw.hello));

String allText = ""; // read entire file

while (scan.hasNextLine()) {
String line = scan.nextLine();
allText += line;
}

myTextView.setText(allText);
scan.close();
Database Management using SQLite
What Is a DBMS?
A database is an organized collection of data.

A database management system (DBMS) is a computer program designed to


manage a database, a large set of structured data, and run operations on the
data requested by numerous users.

Relational Database: A method of structuring data as tables


associated to each other by shared attributes.

A table row corresponds to a unit of data called a record;


a column corresponds to an attribute of that record
Functions of the DBMS

Data Definition Language (DDL):


• Used for defining the database schema
Example:

create table EMP (


EMPNO integer,
ENAME varchar(20),
Job varchar (20) )

Data Manipulation Language (DML):


• Language for Retrieving / Searching/ Accessing and manipulating the
data
• DML generally uses Structured Query Language (SQL)
• SQL is the most widely used query language
• SQL is non-procedural language
Some database software

● Oracle

● Microsoft
– SQL Server (powerful)
– Access (simple)

● PostgreSQL
– powerful/complex free open-source database system

● SQLite
– transportable, lightweight free open-source database system

● MySQL
– simple free open-source database system
– many servers run "LAMP" (Linux, Apache, MySQL, and PHP)
– Wikipedia is run on PHP and MySQL
Android SQLite
SQLite is an open-source relational database i.e. used to perform database
operations on android devices such as storing, manipulating or retrieving
persistent data from the database.

It is embedded in android by default. So, there is no need to perform any


database setup or administration task.
Using Android SQLite Database

General Syntax:
SQLiteDatabase db = openOrCreateDatabase("name", MODE_PRIVATE, null);

db.execSQL("SQL query");
Example: Creating Object for Database

SQLiteDatabase db = openOrCreateDatabase("Teacher", MODE_PRIVATE, null);


Creating Table with column constraints

db.execSQL(“ CREATE TABLE students (


id INTEGER PRIMARY KEY,
name VARCHAR(20) NOT NULL,
email VARCHAR(32),
password VARCHAR(16) NOT NULL
);“ );

● NOT NULL: empty value not allowed in any row for that column
● PRIMARY KEY / UNIQUE: no two rows can have the same value
Example: Creating a Table
Example 1:

db.execSQL("create table teacherTab(name varchar(20))");

Note: If you have created a table once and try to create it again then
Android Studio will give an error

OR
db.execSQL("create table if not exists teacherTab(name varchar(20))");

Example 2:

db.execSQL("create table StudentTab(s_no integer,


name varchar(20))");
Example: Insert data in a table

Note: While inserting data all String values should


be enclosed in single quotes.

Example 1:

db.execSQL("insert into teacherTab( name ) values


(‘Mubeen');");

Example 2:

db.execSQL("insert into StudentTab(s_no, name)


values (30, ‘Danish’);");
Example: Insert data in a table

Example 3: Reading data from variable and inserting


in tables:

int val1=10;
String val2="Mubeen";

db.execSQL("insert into StudentTab(s_no, name)


values (" +val1 +","+ "'"+val2+"'" + ");");
Selecting/Querying data from Tables

In order to select or query data from tables query or rawQuery


functions should be used.

Example:
db.rawQuery("select * from StudentTab", null);

Result of select Query may produce more than one


rows or records.

● Cursor lets you iterate through row results one at a time


– moveToFirst(), moveToPrevious(), moveToNext(),
getColumnCount(), getColumnIndex(name),
getColumnName(index), ….
Using Cursor

Cursor cursor = db.rawQuery("select * from StudentTab", null);

int column=1; // it’s column number you want to extract from table

if (cursor.moveToFirst()) {
do {
String s =cursor.getString(column);

} while (cursor.moveToNext());
}
Example: Select statement using Cursor
Cursor cursor = db.rawQuery("select * from StudentTab", null);

String no="s_no";
String name_id="name";

String s1="";

if (cursor.moveToFirst()) {
do {
s1 = s1 +" "+
cursor.getString(cursor.getColumnIndex(no))+" “
+ cursor.getString(cursor.getColumnIndex(name_id));

} while (cursor.moveToNext());
}

tv1.setText(s1);
Example: Putting it all together (Create, Insert,
and Select Query Examples)
SQLiteDatabase db = openOrCreateDatabase("Teacher", MODE_PRIVATE, null);
//db.execSQL("create table StudentTab(s_no integer, name varchar(20))");

String no="s_no";
int val1=10;

String name_id="name";
String val2="Mubeen";

db.execSQL("insert into StudentTab(" + no + ","+ name_id + ")


values (" +val1 +","+ "'"+val2+"'" + ");");

Cursor cursor = db.rawQuery("select * from StudentTab", null);

String s1="";

if (cursor.moveToFirst()) {
do {
s1 = s1 +" "+ cursor.getString(cursor.getColumnIndex(no))+" “
+ cursor.getString(cursor.getColumnIndex(name_id));

} while (cursor.moveToNext());
}

tv1.setText(s1);

You might also like