SQLite Database Open source, lightweight relational database. Stored in /data/data/package_name/databases Private by default, only accessible by the application that created them. To share a database across applications, use Content Providers.
3
SQLite Database SQLiteOpenHelper wraps up the creating, opening and upgrading of databases. Extends SQLiteOpenHelper and override the Constructor onCreate onUpgrade
4
SQLite Database With an instance of own subclass of SQLiteOpenHelper, call getReadableDatabase or getWritableDatabase to return a read-only or a writable SQLite database Two approaches of using SQLite database to avoid IllegalStateException Open it once and never close it Right before we need to read or write to the database, open it and close it immediately after the operation is done – not possible if using SimpleCursorAdapter
5
public MyDatabaseHelperContext context supercontext, "todo.db", null, 1;
SQLiteOpenHelper constructor 1st parameter, the context, is usually the only parameter for the constructor for our subclass 2nd parameter is the name of the sqlite file 3rd parameter is an instance of CursorFactory, used for creating cursor objects, which usually is set to null 4th parameter is the version of the sqlite database, started from 1
6
@Override public void onCreateSQLiteDatabase db db.execSQL"create table tasks _id " + "integer primary key autoincrement," + "title text, details text;";
SQLiteOpenHelper onCreate Will be called only when the sqlite file e.g. todo.db has not been created Call execSQL and supply the SQL statements to create one or more database tables The field “_id” is required if we are using CursorAdapter to populate a ListView with Cursor
7
@Override public void onUpgradeSQLiteDatabase db, int oldVersion, int newVersion db.execSQL"drop table tasks"; onCreatedb;
SQLiteOpenHelper onUpgrade Will be called only when the sqlite file version number in the constructor is higher than the version number associated to the existing sqlite file created previously with an older version of the app Simplest implementation of a schema change is to drop the existing table and create a new one
8
Writing data to SQLite Database ContentValues are used to insert new rows into database tables or update existing rows. Each ContentValues object represents a single row. The constructor of ContentValues takes an integer to set it's initial size
9
Using ContentValues To add ContentValues values = new ContentValues2; values.put“title”, “Meeting”; values.put“details”, “Financial report”; db.insert“tasks”, null, values; To edit db.update“tasks”, values, “_id=?”, new String[]id; To delete db.delete“tasks”, “_id=?”, new String[]id;
10
SQLite Transactions Call setTransactionSuccessful after all operation in the try block is success and only then endTransaction will commit the changes to the file To avoid scenario that only half of the operation are completed db.beginTransaction; try … db.setTransactionSuccessful; finally db.endTransaction;
11
Getting data from SQLite Database Queries are returned as Cursor objects, which are pointers to a subset of the data. Two ways to execute a query
Cursor cursor = db.rawQuery“select _id, title, ” + “details from tasks order by title”, null;
Cursor cursor = db.query“tasks”, new String[] “_id”, “title”, “details”, null, null, null, null, “title”;
12
Using SimpleCursorAdapter Cursor can be used directly in a SimpleCursorAdapter to populate a ListView The 4th paramater is a string array of the names of the column that their values will populate the TextView with the corresponding resource id in the integer array in the 5th parameter adapter = new SimpleCursorAdapterMainActivity.this, R.layout. row_value , cursor, new String[]"title", "details", new int[]R.id. textViewTitle , R.id. textViewDetails , CursorAdapter. FLAG_REGISTER_CONTENT_OBSERVER ; listView.setAdapteradapter;
13
Using SimpleCursorAdapter OnItemClickListener Call parent.getItemAtPositionIposition to return a particular row of the database table and cast it to a Cursor to retrieve the value of a column, e.g. getString1 – get the string in column 1.
listView.setOnItemClickListenernew AdapterView.OnItemClickListener @Override public void onItemClickAdapterView? parent, View view, int position, long id String title = Cursorparent. getItemAtPositionposition. getString1; ;
Nesta página do site você pode assistir ao vídeo on-line Android Studio - Complete Android Apps Tutorial – SQLite Database - Day 17 - 10.18 duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Rain Forest -热带雨林(RF) 30 Março 2016, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 7 vezes e gostou 0 espectadores. Boa visualização!