SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (20)

Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Android Navigation Component
Android Navigation ComponentAndroid Navigation Component
Android Navigation Component
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
VB net lab.pdf
VB net lab.pdfVB net lab.pdf
VB net lab.pdf
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Android resource
Android resourceAndroid resource
Android resource
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Fragment
Fragment Fragment
Fragment
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferences
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 

Último (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 

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();