sqlite database
DESCRIPTION
SQLite Database. SQLite. Public domain database Advantages Small (about 150 KB) Used on devices with limited resources Each database contained within one file Can be seen in file explorer /data/data/ packagename /database/ Self-contained no special administration needed. SQLite. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/1.jpg)
SQLite Database
![Page 2: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/2.jpg)
SQLite
• Public domain database– Advantages• Small (about 150 KB)
– Used on devices with limited resources
• Each database contained within one file– Can be seen in file explorer
/data/data/packagename/database/
• Self-contained– no special administration needed
![Page 3: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/3.jpg)
SQLite
• Primary components– SQLiteOpenHelper
• android.database.sqlite.SQLiteOpenHelper• manages aspects of the database
– creating or opening database
– SQLiteDatabase• android.database.sqlite.SQLiteDatabase
– methods to interface with database via SQL
– Cursor• android.database.Cursor
– provides access to result set returned by a query
![Page 4: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/4.jpg)
Typical Approaches
• Approach 1: Providing a database with the app– store .db file in Assets folder
• .db file created within Android• .db file created in other SQLite manager, such as Firefox plug-in
– in onCreate() of extended Activity, copy the database if it does not already exist in the device’s internal memory
– onCreate() method in extended SQLiteOpenHelper class typically empty
• Approach 2: Database created when app first run– onCreate() method in extended SQLiteOpenHelper class contains
SQL to create database– will only run if the database does not exist
![Page 5: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/5.jpg)
SQLiteOpenHelper class
![Page 6: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/6.jpg)
SQLiteOpenHelper
• Abstract class– Subclass in a new .java file– Useful methods• onCreate (abstract)
– called when DB first created – table creation done here
• onUpgrade (abstract)– called when DB is changed (i.e. during new app release)– essentially used to drop and recreate database
• getReadableDatabase– called to get a read-only version of database
• getWriteableDatabase– called to get an editable version of database
![Page 7: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/7.jpg)
Sample code - SQLiteOpenHelperpublic class MyClass extends SQLiteOpenHelper { public MyClass (Context ctx) {
//Parameters: Context, file name, CursorFactory factory, database version) //CursorFactory rarely used – only used for customized Cursors super (ctx, "myfilename.db", null, 1);
}
public void onUpgrade (SQLiteDatabase db, int oldVer, int newVer) { db.execSQL("DROP TABLE IF EXISTS TABLE_NAME"); onCreate(db);
}
public void onCreate(SQLiteDatabase db) { //_id field must be included!!!
db.execSQL("CREATE TABLE MYTABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT, ..."); }}
![Page 8: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/8.jpg)
SQLiteDatabase class
![Page 9: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/9.jpg)
SQLiteDatabase
• Concrete class– SQL methods
• Convenience methods– query – returns a Cursor– insert – returns _id of new record or –1 if error occurred– update – returns number of rows affected– delete – number of rows affected (0 if no where clause)
» pass ‘1’ as where clause to get count in this case
• Manual creation of SQL– rawQuery
» can execute any SELECT statement and returns a Cursor– execSQL
» can execute any SQL statement that is not a SELECT• void return type
![Page 10: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/10.jpg)
SQLiteDatabase
• query method– Parameters
• String – table name• String [] – columns to select
– _id field must be included if displaying in List
• String – where clause (without the word WHERE)– null if not wanted– ? for arguments
• String [] – selection clause, to allow arguments for field names in where clause– only if ? in preceding string
• String – group by clause (without the words GROUP BY)• String – having clause (without the word HAVING) • String – order by clause (without the words ORDER BY)
![Page 11: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/11.jpg)
Sample code – query
String [] FROM = {List of desired columns to select};
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getReadableDatabase();
Cursor myCursor = db.query(MY_TABLE_NAME, FROM, null, null, null, null, null);
![Page 12: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/12.jpg)
SQLiteDatabase
• insert method– Parameters• String – table name• String – work around to allow insertion of empty row in SQL
– nullColumnHack– SQLLite does not allow:
» INSERT INTO MYTABLE– ContentValues will be null
• ContentValues – Object holding the corresponding field names and values
![Page 13: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/13.jpg)
Sample code – insert
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, column value);values.put(COLUMN_NAME, column value);...values.put(COLUMN_NAME, column value);
db.insert(MY_TABLE_NAME, null, values);
![Page 14: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/14.jpg)
SQLiteDatabase
• update method– Parameters• String – table name• ContentValues – Object holding the corresponding field names
and values • String – where clause (without the word WHERE)• String [] – selection clause, to allow arguments for field names
in where clause
![Page 15: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/15.jpg)
Sample code – update
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase();
String where = "My WHERE clause (without the word WHERE)";ContentValues values = new ContentValues();
values.put(COLUMN_NAME, column value);values.put(COLUMN_NAME, column value);...values.put(COLUMN_NAME, column value);
db.update(MY_TABLE_NAME, values, where, null);
![Page 16: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/16.jpg)
SQLiteDatabase
• delete method– Parameters• String – table name• String – where clause (without the word WHERE)• String [] – selection clause, to allow arguments for field names
in where clause
![Page 17: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/17.jpg)
Sample code – delete
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase();
String where = "My WHERE clause (without the word WHERE)";
db.delete(MY_TABLE_NAME, where, null);
![Page 18: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/18.jpg)
SQLiteDatabase
• rawQuery method– Parameters• String – SQL statement• String [] – selection clause, to allow arguments for field names
in where clause
![Page 19: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/19.jpg)
Sample code – rawQuery
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase();
Cursor myCursor = db.rawQuery(“SQL statement here”, null);
![Page 20: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/20.jpg)
SQLiteDatabase
• execSQL method– Parameters• String – SQL statement
![Page 21: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/21.jpg)
Sample code – execSQL
SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase();
db.execSQL(“SQL statement here”);
![Page 22: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/22.jpg)
Cursor class
![Page 23: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/23.jpg)
Aspects of Cursor class
• SQLiteDatabase query method returns a Cursor– Cursor must be managed by Activity• Cursor class allows manipulation of pointer
– move, moveToFirst, moveToNext, moveToPosition, etc.
• Cursor class allows retrieval of data– getInt, getDouble, getString, etc.– must provide column position
• Cursor can be associated with an Adapter– typically SimpleCursorAdapter
![Page 24: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/24.jpg)
Sample code – Cursor data retrieval
• Retrieving the integer in the 4th row, 3rd column
myCursor.moveToPosition(3);int x = myCursor.getInt(2);
![Page 25: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/25.jpg)
Sample code – populating a Cursor from a Database
private Cursor populateCursor() {String [] FROM = {List of desired DB field names};
SQLiteDatabase db = myInstanceOfSQLiteHelper.getReadableDatabase();
Cursor myCursor = db.query(MY_TABLE_NAME, FROM, null, null, null, null, null);
return myCursor;}
![Page 26: SQLite Database](https://reader036.vdocuments.site/reader036/viewer/2022081417/56813c69550346895da5fabf/html5/thumbnails/26.jpg)
Sample code – displaying Cursor contents in a ListView
private void displayRecords(Cursor c) {String [] from = {List of desired field names to display};int [] to = new int [] {List of TextViews to display field names (i.e. R.id.myTextView)};
ListView lv = (ListView)findViewById(R.id.myListView);
SimpleCursorAdapter records = new SimpleCursorAdapter(this, R.layout.my_file_for_TextViews, c, from, to);
lv.setAdapter(records);}