Pertama bukalah project Database1 anda di eclipse.
1. DBAdapter.java
Lihatlah isi file DBAdapter.java anda, tidak ada yang perlu di ubah pada
file ini namun disini kami akan menjelaskan file ini. lihatlah
sourcenya di bawah :
package com.wilis.database1; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_ISBN = "isbn"; public static final String KEY_TITLE = "title"; public static final String KEY_PUBLISHER = "publisher"; public static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "books"; private static final String DATABASE_TABLE = "titles"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table titles(_id integer primary key autoincrement, " + "isbn text not null, title text not null, "+"publisher text not null);"; private final Context context; private static DatabaseHelper DBHelper; private static SQLiteDatabase db; public DBAdapter(Context ctx){ this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db){ db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ Log.w(TAG, "Upgrading database from version " + oldVersion +" To "+ newVersion +" , wich will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS titles"); onCreate(db); } } public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } public void close(){ DBHelper.close(); } public long insertTitle(String isbn, String title, String publisher){ ContentValues initialValues = new ContentValues(); initialValues.put(KEY_ISBN, isbn); initialValues.put(KEY_TITLE, title); initialValues.put(KEY_PUBLISHER, publisher); return db.insert(DATABASE_TABLE, null, initialValues); } public boolean deleteTitle(long rowId){ return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } public Cursor getAllTitles(){ return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER }, null, null, null, null, null); } public Cursor getTitle(long rowId) throws SQLException{ Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]{ KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER }, null, null, null, null, null, null); if(mCursor != null){ mCursor.moveToFirst(); } return mCursor; } public boolean updateTitle(long rowId, String isbn, String title, String publisher){ ContentValues args = new ContentValues(); args.put(KEY_ISBN, isbn); args.put(KEY_TITLE, title); args.put(KEY_PUBLISHER, publisher); return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; } }
a. Perhatikan script berikut :
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
Inilah library yan digunakan untuk menghubungkan aplikasi dengan SQLite.
b. Kemudian untuk nama database, version, dan tabelnya didefinisikan disini :
public static final String KEY_ROWID = "_id"; public static final String KEY_ISBN = "isbn"; public static final String KEY_TITLE = "title"; public static final String KEY_PUBLISHER = "publisher"; public static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "books"; private static final String DATABASE_TABLE = "titles"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table titles(_id integer primary key autoincrement, " + "isbn text not null, title text not null, "+"publisher text not null);"; private final Context context;
dan juga disini :
public void onCreate(SQLiteDatabase db){ db.execSQL(DATABASE_CREATE); }
Nama databasenya adalah books dengan field-field id, isbn, title, publisher beserta tipe fieldnya.
c. Kemudian untuk fungsi open database menggunakan script berikut :
public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; }
d. Untuk fungsi insert data :
public long insertTitle(String isbn, String title, String publisher){ ContentValues initialValues = new ContentValues(); initialValues.put(KEY_ISBN, isbn); initialValues.put(KEY_TITLE, title); initialValues.put(KEY_PUBLISHER, publisher); return db.insert(DATABASE_TABLE, null, initialValues); }
e. Untuk mendelete data
public boolean deleteTitle(long rowId){ return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; }
f. Untuk menampilkan semua data
public Cursor getAllTitles(){ return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER }, null, null, null, null, null); }
g. Untuk mengupdate data
public boolean updateTitle(long rowId, String isbn, String title, String publisher){ ContentValues args = new ContentValues(); args.put(KEY_ISBN, isbn); args.put(KEY_TITLE, title); args.put(KEY_PUBLISHER, publisher); return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; }
2. Database1.java
Untuk mengupdate data ubahlah isi script database1.java dalam direktori src/com.wilis.database1 menjadi seperti ini :package com.wilis.database1; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.widget.Toast; public class database1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); db.open(); if (db.updateTitle(1, "1234", "Programmer Aplikasi Android", "Toples Programer")) { Toast.makeText(this, "Update Berhasil", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Update Gagal", Toast.LENGTH_LONG).show(); } Cursor c = db.getTitle(1); if (c.moveToFirst()) DisplayTitle(c); else Toast.makeText(this, "Title tidak ditemukan", Toast.LENGTH_LONG) .show(); db.close(); } private void DisplayTitle(Cursor c) { // TODO Auto-generated method stub Toast.makeText( this, "id " + c.getString(0) + "\n" + "ISBN : " + c.getString(1) + "\n" + "TITLE : " + c.getString(2) + "\n" + "Publisher : " + c.getString(3), Toast.LENGTH_LONG) .show(); } }
Sekarang coba anda jalankan aplikasi anda dengan klik Run > Run As Android Aplication.
Sekarang coba anda cek kembali SQLite Database anda.
Jika pada aplikasi anda muncul pesan update berhasil dan data pada SQLite Database anda berubah maka anda berhasil.
Selanjutnya jika anda mau menghapus data yang ada pada datbase anda, anda dapat mengganti kodenya dengan script berikut :
db.open();
if(db.deleteTitle(1))
Toast.makeText(this, "Penghapusan Berhasil", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Penghapusan Gagal", Toast.LENGTH_LONG).show();
db.close();
Sedangkan jika anda ingin melihat semua isi data yang ada di database anda dapat menggantinya dengan kode di bawah :
db.open();
Cursor c = db.getAllTitles();
if(c.moveToFirst()){
do{
DisplayTitle(c);
}while(c.moveToNext());
}else{
Toast.makeText(this, "kga ad", Toast.LENGTH_LONG).show();
}
db.close();
Sumber : Pemograman Aplikasi Mobile Smartphone dan tablet PC Berbasic Android Penerbit Informatika, Bandung, 2012 By : Nazruddin Safaat.
0 Komentar untuk "Membuat Aplikasi Android Operasi Database "