SlideShare a Scribd company logo
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

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