SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Applets in java
Applets in javaApplets in java
Applets in java
 
Json
JsonJson
Json
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Android intents
Android intentsAndroid intents
Android intents
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Android adapters
Android adaptersAndroid adapters
Android adapters
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
Dom
DomDom
Dom
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Android Layout.pptx
Android Layout.pptxAndroid Layout.pptx
Android Layout.pptx
 
String in java
String in javaString in java
String in java
 
Applets
AppletsApplets
Applets
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 

Similar a SQLITE Android

Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptxShshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
086ChintanPatel1
 
My sql and jdbc tutorial
My sql and jdbc tutorialMy sql and jdbc tutorial
My sql and jdbc tutorial
Manoj Kumar
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
priya951125
 

Similar 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
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
 
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
 

Más de Sourabh Sahu

Más 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

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
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
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Último (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 
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
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 

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