INTRODUCTION TO ANDROID MOBILE APP DEVELOPMENT MUHAMMAD BAGIR., MTI
MATERI • Pengenalan SQLite
• SQLite Open Helper • Fungsi-fungsoi SQLite
SQLite Database SQLite Database Browser
SQLite Database(cont..) Used as usual database, usually to store small data. (For larger data, it’s encouraged to use web service). The SQLite Database file will be created the first time application started and the database can be upgraded for further use
SQLite Data type: • Text • Numeric • BLOB • Integer primary key
SQLiteOpenHelper public class HelloSQLite extends SQLiteOpenHelper{ public static String dbName = "test.sqlite"; public static int dbVersion = 1; public HelloSQLite(Context context){ super(context, dbName, null, dbVersion); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
For create a table, use usual SQL Sintax: String sql = “create table
( , , . . . . . . )”; db.execSQL(sql);
For inserting a row into a table: ContentValues initialValues = new ContentValues(); initialValues.put(, ’'); initialValues.put(, ’'); db.insert(, null, initialValues);
For deleting row from a table: String sql = “DELETE from WHERE ”; db.execSQL(sql);
MEMBUAT OBJEK DAN MEMASUKKAN DATA •
•
Deklarasikan Nama Database, Context dan Version dengan public modifier static pada field static String dbName="ContohDB"; static CursorFactory factory; static int dbVersion=1; super(context, dbName, factory, dbVersion); Syntax membuat tabel pada metode onCreate: String tabelArtist = "CREATE TABLE data_artist" +"(IDS INTEGER PRIMARY KEY," + "ARTIST_NAME TEXT)"; dbObject.execSQL(tabelArtist);
• Sebelum dapat memasukkan data ke tabel, buatlah class model setter getter untuk proses data tabel artist •
Syntax memasukkan data ke tabel: public void addArtist(Artist art) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(kolom2, art.getArtists()); db.insert(namaTabel, null, values); db.close(); } • Panggil dari Activity dengan : dt.addArtist(new Artist(myEditText.getText().toString()));
Selecting a table in SQLite Database return a Cursor, which can be used to fetching the data for later use. For selecting a SQLiteDatabase table in java: Cursor cursor = db.query(, new String[]{, }, <where_clause>, <selectionArgs>, , , );
For fetching the result: while(cursor.moveToNext()){ cursor.getString(column_index1); cursor.getString(column_index2); . . . }
MENAMPILKAN DATA DARI DATABASE •
•
Metode untuk menampilkan data public List getArtist() { List artList = new ArrayList(); String kueriArtis = "Select * from "+namaTabel; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(kueriArtis, null); //memulai proses pengambilan data dan melakukan looping if (cursor.moveToFirst()) { do { Artist art = new Artist(); //art.(cursor.getString(2)); art.setArtists(cursor.getString(1)); artList.add(art); } while(cursor.moveToNext()); } return artList; } Memanggil metode dan menambahkan ke ListView List art = dt.getArtist(); for(Artist obj: art) { toDo.add(0,obj.getArtists().toString()); }
Note: • Before starting any database action, the database must be opened, and closed after finished. dbHelper.getReadableDatabase(); Or dbHelper.getWriteableDatabase(); db.close(); • Database will be stored in: /data/data/YOUR_PACKAGE/databases/DATABASE_NAME
Other way to use SQLite Database: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-androidapplications/
MENAMBAHKAN PROSES GET ID ARTIS •
Untuk Mendapatkan ID Artis dari tabel bagi kebutuhan proses update maka harus dilakukan penambahan adapter yang dapat diget saat item diklik
•
Pada Field
ArrayList idArtis; ArrayAdapter bb; •
Pada Content View
idArtis = new ArrayList(); bb = new ArrayAdapter(this,android.R.layout.simple_selectable_list_item,idArtis);
myListView.setAdapter(bb); •
Pada Loop
idArtis.add(0, obj.getIdArt());
•
MENAMBAHKAN PROSES UPDATE
Metode untuk update tabel Artis
public void updateArtist(Artist art) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(kolom2, art.getArtists()); String artis=Integer.toString(art.getIdArt ());// updating row db.update(namaTabel, values, kolom1 + "= ?", new String[] { String.valueOf(artis) }); }
Metode pada Activity untuk memanggil proses update public void updateData(View v) { dt.updateArtist(new Artist(myEditText.getText().toStrin g(),idArt)); aa.clear(); aa.notifyDataSetChanged(); art = dt.getArtist(); for(Artist obj: art) { String dataArtists=obj.getArtists().toString(); idArtis.add(0, obj.getIdArt()); toDo.add(0,dataArtists); //toDo.add(0,Integer.toString(obj.getI dArt())); } myEditText.setText(""); }
PROSES DELETE •
Untuk metode Proses delete tambahkan metode pada dao sbb :
public void deleteArtist(Artist arts) { SQLiteDatabase db = this.getWritableDatabase(); String idArtis=Integer.toString(arts.getIdArt());
// updating row db.delete(namaTabel, kolom1 + "= ?", new String[] { String.valueOf(idArtis) }); }