SlideShare a Scribd company logo
1 of 22
SQLite
1
Sourabh Sahu
SQLite

Created by D. Richard Hipp

Offline Applications

Permanent Storage

SQLite tools

Small Size around 0.5 MB

Entire database in a single file
• Used In Stand alone applications
• Local database cache
• Embedded devices
• Internal or temporary databases
• Application file format
• Server less applications
• NULL – null value
• INTEGER - signed integer, stored in 1, 2, 3, 4, 6, or 8
bytes depending on the magnitude of the value
• REAL - a floating point value, 8-byte IEEE floating point
number.
• TEXT - text string, stored using the database encoding
(UTF-8, UTF-16BE or UTF-16LE).
• BLOB. The value is a blob of data, stored exactly as it was
input.
Column Data Type
SQLite Classes
• SQLiteCloseable - An object created from a SQLiteDatabase that can be
closed.
• SQLiteCursor - A Cursor implementation that exposes results from a query
on a SQLiteDatabase.
• SQLiteDatabase - Exposes methods to manage a SQLite database.
• SQLiteOpenHelper - A helper class to manage database creation and
version management.
• SQLiteProgram - A base class for compiled SQLite programs.
• SQLiteQuery - A SQLite program that represents a query that reads the
resulting rows into a CursorWindow.
• SQLiteQueryBuilder - a convenience class that helps build SQL queries to
be sent to SQLiteDatabase objects.
• SQLiteStatement - A pre-compiled statement against a SQLiteDatabase
that can be reused.
SQLiteDatabase
• Contains the methods for: creating, opening,
closing, inserting, updating, deleting and quering
an SQLite database
• These methods are similar to JDBC but more
method oriented than what we see with JDBC
(remember there is not a RDBMS server running)
• SQLiteDatabase db;
• db= openOrCreateDatabase
("my_sqlite_database.db" ,
SQLiteDatabase.CREATE_IF_NECESSARY , null);
CREATE
• Create a static string containing the SQLite
CREATE statement, use the execSQL( ) method
to execute it.
• String studentcreate= "CREAT TABLE students(
id INTEGER PRIMARY KEY AUTOINCREMENT,
fname TEXT, lname TEXT,age INTEGER,mob
TEXT
• );
• db.execSQL(studentcreate);
ContentValues values = new ContentValues( );
• values.put("firstname" , “Ram");
• values.put("lastname" , “Sharma");
• values.put(“age" , “13");
values.put(“mob" , “9479864026");
• long id = myDatabase.insert(“students" , "" ,
values);
INSERT
Update
• public void updateFNameTitle(Integer id, String
newName) {
• ContentValues values = new ContentValues();
• values.put(“fname" , newName);
• myDatabase.update(“students" , values ,
• "id=?" , new String[ ] {id.toString() } );
• }
DELETE
• public void deleteStudents(Integer id) {
• myDatabase.delete(“students" , "id=?"
,
• new String[ ] { id.toString( ) } ) ;
• }
SELECT
• SQL - "SELECT * FROM Students;"
Cursor c =db.query(students,null,null,null,null,null,null);
• SQL - "SELECT * FROM Students WHERE id=5"
Cursor c = db.query(Students ,null,“id=?" , new String[ ]
{"5"},null,null,null);
• SQL – "SELECT fname,id FROM Students ORDER BY fname
ASC"
SQLite – String colsToReturn [ ] {“fname","id"};
String sortOrder = “fname ASC";
Cursor c = db.query(“Students",colsToReturn,
null,null,null,null,sortOrder);
How to Do it
Create a class DataBaseHelper
DEFINE FEILDS
CREATE STATEMENT
CREATE
POJO
Adding Student from Table
• // Adding new Student
void addStudent(Student s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_F_NAME, s.getfName()); // Name
values.put(KEY_L_NAME, s.getlName()); //
values.put(KEY_MOB, s.getMob());
values.put(KEY_AGE, s.getAge());
// Inserting Row
db.insert(TABLE_STUDENTS, null, values);
db.close(); // Closing database connection
}
Getting Student from Table
•
//int id, String fName, String lName, String mob, int age
// Getting single contact
Student getStudent(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_STUDENTS, new String[] { KEY_ID,
KEY_F_NAME,KEY_L_NAME,KEY_MOB,KEY_MOB }, KEY_ID +
"=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Student s1 = new Student(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),cursor.getString(3),Integer.parseInt(cursor.getString(4)));
// return contact
return s1;
}
Getting All Student List
• // Getting All Contacts
public List<Student> getAllStudents() {
List<Student> studentList = new ArrayList<Student>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_STUDENTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Student s1 = new Student(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),cursor.getString(3),Integer.parseInt(cursor.getString(4)));
studentList.add(s1);
} while (cursor.moveToNext());
}
// return contact list
return studentList;
}
Update
•
// Updating single Student
public int updateStudent(Student student) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_F_NAME, student.getfName());
values.put(KEY_L_NAME, student.getlName());
values.put(KEY_MOB, student.getMob());
values.put(KEY_AGE, student.getAge());
// updating row
return db.update(TABLE_STUDENTS, values, KEY_ID
+ " = ?",
new String[] { String.valueOf(student.getId()) });
}
Deletion and count
// Deleting single Student
public void deleteStudent(Student contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_STUDENTS, KEY_ID + " = ?",
new String[]
{ String.valueOf(contact.getId()) });
db.close();
}
// Getting contacts Count
public int getStudentCount() {
String countQuery = "SELECT * FROM " +
TABLE_STUDENTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
Coding on Activity Side
Thank You
22

More Related Content

What's hot (20)

Java swing
Java swingJava swing
Java swing
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
C# basics
 C# basics C# basics
C# basics
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Database in Android
Database in AndroidDatabase in Android
Database in Android
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 

Similar to SQLITE Android

Sqlite
SqliteSqlite
SqliteKumar
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile appsIvano Malavolta
 
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptxShshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx086ChintanPatel1
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorialinfo_zybotech
 
My sql and jdbc tutorial
My sql and jdbc tutorialMy sql and jdbc tutorial
My sql and jdbc tutorialManoj Kumar
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web appsIvano Malavolta
 
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...Howard Greenberg
 
Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming TechniquesRaji Ghawi
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile appsIvano Malavolta
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/HibernateSunghyouk Bae
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Khaled Anaqwa
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101Jollen Chen
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptxnatesanp1234
 
MongoDB Knowledge share
MongoDB Knowledge shareMongoDB Knowledge share
MongoDB Knowledge shareMr Kyaing
 

Similar to SQLITE Android (20)

Sqlite
SqliteSqlite
Sqlite
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps
 
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptxShshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
Python with MySql.pptx
Python with MySql.pptxPython with MySql.pptx
Python with MySql.pptx
 
My sql and jdbc tutorial
My sql and jdbc tutorialMy sql and jdbc tutorial
My sql and jdbc tutorial
 
Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web apps
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
 
Local Storage
Local StorageLocal Storage
Local Storage
 
Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
 
Data base.ppt
Data base.pptData base.ppt
Data base.ppt
 
MongoDB Knowledge share
MongoDB Knowledge shareMongoDB Knowledge share
MongoDB Knowledge share
 

More from Sourabh Sahu

Understanding GIT and Version Control
Understanding GIT and Version ControlUnderstanding GIT and Version Control
Understanding GIT and Version ControlSourabh Sahu
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization Sourabh Sahu
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick GuideSourabh Sahu
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themesSourabh Sahu
 
SeekBar in Android
SeekBar in AndroidSeekBar in Android
SeekBar in AndroidSourabh Sahu
 
Android ListView and Custom ListView
Android ListView and Custom ListView Android ListView and Custom ListView
Android ListView and Custom ListView Sourabh Sahu
 
Android project architecture
Android project architectureAndroid project architecture
Android project architectureSourabh Sahu
 
Shared preferences
Shared preferencesShared preferences
Shared preferencesSourabh Sahu
 
Content Providers in Android
Content Providers in AndroidContent Providers in Android
Content Providers in AndroidSourabh Sahu
 
Calendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePickerCalendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePickerSourabh Sahu
 
Progress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialogProgress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialogSourabh Sahu
 
AutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextViewAutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextViewSourabh Sahu
 
Parceable serializable
Parceable serializableParceable serializable
Parceable serializableSourabh Sahu
 
Android Architecture
Android ArchitectureAndroid Architecture
Android ArchitectureSourabh Sahu
 
Android Installation Testing
Android Installation TestingAndroid Installation Testing
Android Installation TestingSourabh Sahu
 
Android Installation
Android Installation Android Installation
Android Installation Sourabh Sahu
 

More from Sourabh Sahu (20)

Understanding GIT and Version Control
Understanding GIT and Version ControlUnderstanding GIT and Version Control
Understanding GIT and Version Control
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick Guide
 
Python Course
Python CoursePython Course
Python Course
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
 
SeekBar in Android
SeekBar in AndroidSeekBar in Android
SeekBar in Android
 
Android layouts
Android layoutsAndroid layouts
Android layouts
 
Android ListView and Custom ListView
Android ListView and Custom ListView Android ListView and Custom ListView
Android ListView and Custom ListView
 
Activities
ActivitiesActivities
Activities
 
Android project architecture
Android project architectureAndroid project architecture
Android project architecture
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 
Content Providers in Android
Content Providers in AndroidContent Providers in Android
Content Providers in Android
 
Calendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePickerCalendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePicker
 
Progress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialogProgress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialog
 
AutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextViewAutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextView
 
Web view
Web viewWeb view
Web view
 
Parceable serializable
Parceable serializableParceable serializable
Parceable serializable
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
 
Android Installation Testing
Android Installation TestingAndroid Installation Testing
Android Installation Testing
 
Android Installation
Android Installation Android Installation
Android Installation
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

SQLITE Android

  • 2. SQLite  Created by D. Richard Hipp  Offline Applications  Permanent Storage  SQLite tools  Small Size around 0.5 MB  Entire database in a single file
  • 3. • Used In Stand alone applications • Local database cache • Embedded devices • Internal or temporary databases • Application file format • Server less applications
  • 4. • NULL – null value • INTEGER - signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value • REAL - a floating point value, 8-byte IEEE floating point number. • TEXT - text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE). • BLOB. The value is a blob of data, stored exactly as it was input. Column Data Type
  • 5. SQLite Classes • SQLiteCloseable - An object created from a SQLiteDatabase that can be closed. • SQLiteCursor - A Cursor implementation that exposes results from a query on a SQLiteDatabase. • SQLiteDatabase - Exposes methods to manage a SQLite database. • SQLiteOpenHelper - A helper class to manage database creation and version management. • SQLiteProgram - A base class for compiled SQLite programs. • SQLiteQuery - A SQLite program that represents a query that reads the resulting rows into a CursorWindow. • SQLiteQueryBuilder - a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects. • SQLiteStatement - A pre-compiled statement against a SQLiteDatabase that can be reused.
  • 6. SQLiteDatabase • Contains the methods for: creating, opening, closing, inserting, updating, deleting and quering an SQLite database • These methods are similar to JDBC but more method oriented than what we see with JDBC (remember there is not a RDBMS server running) • SQLiteDatabase db; • db= openOrCreateDatabase ("my_sqlite_database.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null);
  • 7. CREATE • Create a static string containing the SQLite CREATE statement, use the execSQL( ) method to execute it. • String studentcreate= "CREAT TABLE students( id INTEGER PRIMARY KEY AUTOINCREMENT, fname TEXT, lname TEXT,age INTEGER,mob TEXT • ); • db.execSQL(studentcreate);
  • 8. ContentValues values = new ContentValues( ); • values.put("firstname" , “Ram"); • values.put("lastname" , “Sharma"); • values.put(“age" , “13"); values.put(“mob" , “9479864026"); • long id = myDatabase.insert(“students" , "" , values); INSERT
  • 9. Update • public void updateFNameTitle(Integer id, String newName) { • ContentValues values = new ContentValues(); • values.put(“fname" , newName); • myDatabase.update(“students" , values , • "id=?" , new String[ ] {id.toString() } ); • }
  • 10. DELETE • public void deleteStudents(Integer id) { • myDatabase.delete(“students" , "id=?" , • new String[ ] { id.toString( ) } ) ; • }
  • 11. SELECT • SQL - "SELECT * FROM Students;" Cursor c =db.query(students,null,null,null,null,null,null); • SQL - "SELECT * FROM Students WHERE id=5" Cursor c = db.query(Students ,null,“id=?" , new String[ ] {"5"},null,null,null); • SQL – "SELECT fname,id FROM Students ORDER BY fname ASC" SQLite – String colsToReturn [ ] {“fname","id"}; String sortOrder = “fname ASC"; Cursor c = db.query(“Students",colsToReturn, null,null,null,null,sortOrder);
  • 12. How to Do it Create a class DataBaseHelper
  • 16. Adding Student from Table • // Adding new Student void addStudent(Student s) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_F_NAME, s.getfName()); // Name values.put(KEY_L_NAME, s.getlName()); // values.put(KEY_MOB, s.getMob()); values.put(KEY_AGE, s.getAge()); // Inserting Row db.insert(TABLE_STUDENTS, null, values); db.close(); // Closing database connection }
  • 17. Getting Student from Table • //int id, String fName, String lName, String mob, int age // Getting single contact Student getStudent(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_STUDENTS, new String[] { KEY_ID, KEY_F_NAME,KEY_L_NAME,KEY_MOB,KEY_MOB }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Student s1 = new Student( Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3),Integer.parseInt(cursor.getString(4))); // return contact return s1; }
  • 18. Getting All Student List • // Getting All Contacts public List<Student> getAllStudents() { List<Student> studentList = new ArrayList<Student>(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_STUDENTS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { Student s1 = new Student( Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3),Integer.parseInt(cursor.getString(4))); studentList.add(s1); } while (cursor.moveToNext()); } // return contact list return studentList; }
  • 19. Update • // Updating single Student public int updateStudent(Student student) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_F_NAME, student.getfName()); values.put(KEY_L_NAME, student.getlName()); values.put(KEY_MOB, student.getMob()); values.put(KEY_AGE, student.getAge()); // updating row return db.update(TABLE_STUDENTS, values, KEY_ID + " = ?", new String[] { String.valueOf(student.getId()) }); }
  • 20. Deletion and count // Deleting single Student public void deleteStudent(Student contact) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_STUDENTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getId()) }); db.close(); } // Getting contacts Count public int getStudentCount() { String countQuery = "SELECT * FROM " + TABLE_STUDENTS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); cursor.close(); // return count return cursor.getCount();