ANDROID SQLite Database
SQLite adalah database yang bisa dibangun di android. SQLite
memiliki fitur relasional database, hampir sama dengan SQL pada
desktop hanya saja SQLite membutuhkan memori yang sedikit.
SQLite
terdapat
pada
semua
perangkat
android,
cukup
mendefinisikan perintah SQL untuk meng-create atau
meng-update
database, selanjutnya system pda android akan menangani hal-hal yang
berhubungan dengan database. SQLite database otomatis akan tersimpan
didalam path data/data/nama_package/database/nama_database.
Sebelum mulai membuat aplikasi SQLite database, ada 3
kelas yang harus Kamu ketahui yaitu
1.
SQLiteDatabase
SQLiteDatabase merupakan kelas yang mempunyai method seperti
2.
Insert() untuk menambahkan baris ke database
Update() untuk memperbarui baris pada database
Delete() untuk menghapus baris pada database
execSQL() untuk mengeksekusi sintak SQL
SQLiteOpenHelper
SQLiteOpenHelper adalah subclass yang memiliki beberapa method seperti
3.
onCreate() dijalankan jika sebelumnya belum ada database
onUpgrade() dijalankan jika sebelumnya sudah ditemukan database
yang sama namun beda versi. Method ini bisa dimanfaatkan untuk
mengubah skema database.
onOpen() dijalankan jika database sudah dalam keadaan open
getWritableDatabase() memanggil database agar bisa dimasuki data
getReadableDatabase() memanggil database agar bisa membaca
datanya
Cursor
Setiap query yang
feedback. Feedback
dieksekusi pasti membawa nilai kembalian atau
yang dihasilkan query ini disebut [Link]
dengan kata lain, cursor merepresentasikan hasil query yang dieksekusi
pada baris dan kolom tertentu. Tabel 1.1 berikut ini akan membantu
Kamu memahami istilah cursor pada SQLite.
Nama
Anang
Dodi
Arif
Qosim
Hobi
Baca
Futsal
Lari
Baca
[Arif,Lari]
Tabel 1.1. Analogi cursor pada SQLite database
Cursor diwakili dengan tKamu
panah berada pada posisi baris ke 2
dengan membawa data [arif,lari]. Berikut ini beberapa method yang
digunakan dalam latihan ini
moveToFirst() untuk pindah ke baris pertama
isAfterLast() akan mengirim pesan balik jika posisi cursor sudah
berada di baris terakhir
getLong() untuk mengambil data pada kolom yang
mempunyai tipe data long
getString() untuk mengambil data pada kolom yang mempunyai tipe
data String
Selanjutnya
akan
AsiaStudentRecord
membuat
Project
Android
Baru
Pertama Kita akan Membuat Splash activity untuk splash screen
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/asia" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
Bernama
android:textSize="16dp"
android:textColor="#454545"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Powered By [Link]" />
</RelativeLayout>
Setelah Membuat Activity splash sebagai Activity yang akan dijalankan
pertama kali harus membuat perubahan pada manifest android sebagai
berikut
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">
<application
android:allowBackup="true"
android:icon="@drawable/asia"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Splash">
<intent-filter>
<action android:name="[Link]" />
<category
android:name="[Link]" />
</intent-filter>
</activity>
<activity android:name=".BuatBiodata"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
<activity android:name=".LihatBiodata"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
<activity android:name=".UpdateBiodata"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
</application>
</manifest>
Selanjutnya membuat activity_main.xml
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ecf0f1"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="?android:attr/borderlessButtonStyle"
android:drawableLeft="@drawable/ic_note_add_black_24dp"
android:text="Buat Biodata Baru" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
<ImageView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/asia"
android:alpha="0.4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
[Link]
package [Link];
import
import
import
import
import
import
import
import
import
import
import
import
import
import
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
public class MainActivity extends AppCompatActivity {
String[] daftar;
ListView ListView01;
Menu menu;
protected Cursor cursor;
DataHelper dbcenter;
public static MainActivity ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button btn=(Button)findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent inte = new Intent([Link],
[Link]);
startActivity(inte);
}
});
ma = this;
dbcenter = new DataHelper(this);
RefreshList();
}
public void RefreshList(){
SQLiteDatabase db = [Link]();
cursor = [Link]("SELECT * FROM biodata",null);
daftar = new String[[Link]()];
[Link]();
for (int cc=0; cc < [Link](); cc++){
[Link](cc);
daftar[cc] = [Link](1).toString();
}
ListView01 = (ListView)findViewById([Link].listView1);
[Link](new
ArrayAdapter(this,
[Link].simple_list_item_1, daftar));
[Link](true);
[Link](new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int
arg2, long arg3) {
final String selection = daftar[arg2];
//.getItemAtPosition(arg2).toString();
final CharSequence[] dialogitem = {"Lihat Biodata",
"Update Biodata", "Hapus Biodata"};
[Link] builder = new
[Link]([Link]);
[Link]("Pilihan");
[Link](dialogitem, new
[Link]() {
public void onClick(DialogInterface dialog, int
item) {
switch(item){
case 0 :
Intent i = new
Intent(getApplicationContext(), [Link]);
[Link]("nama", selection);
startActivity(i);
break;
case 1 :
Intent in = new
Intent(getApplicationContext(), [Link]);
[Link]("nama", selection);
startActivity(in);
break;
case 2 :
SQLiteDatabase db =
[Link]();
[Link]("delete from biodata where
nama = '"+selection+"'");
RefreshList();
break;
}
}
});
[Link]().show();
}});
((ArrayAdapter)[Link]()).notifyDataSetInvalida
ted();
}}
Membuat [Link]
package [Link];
/**
* Created by Gigabyte on 02/11/2016.
*/
import
import
import
import
[Link];
[Link];
[Link];
[Link];
public class DataHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "[Link]";
private static final int DATABASE_VERSION = 1;
public DataHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "create table biodata(no integer primary key,
nama text null, tgl text null, jk text null, alamat text null);";
Log.d("Data", "onCreate: " + sql);
[Link](sql);
sql = "INSERT INTO biodata (no, nama, tgl, jk, alamat) VALUES
('1001', 'Fathur', '1994-02-03', 'Laki-laki','Jakarta');";
[Link](sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
[Link] class ini digunakan untuk membuat/insert data
package [Link];
/**
* Created by Gigabyte on 02/11/2016.
*/
import
import
import
import
import
import
import
import
import
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
public class BuatBiodata extends AppCompatActivity {
protected Cursor cursor;
DataHelper dbHelper;
Button ton1, ton2;
EditText text1, text2, text3, text4, text5;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_buat_biodata);
dbHelper = new DataHelper(this);
text1 = (EditText) findViewById([Link].editText1);
text2 = (EditText) findViewById([Link].editText2);
text3 = (EditText) findViewById([Link].editText3);
text4 = (EditText) findViewById([Link].editText4);
text5 = (EditText) findViewById([Link].editText5);
ton1 = (Button) findViewById([Link].button1);
ton2 = (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db = [Link]();
[Link]("insert into biodata(no, nama, tgl, jk,
alamat) values('" +
[Link]().toString()+"','"+
[Link]().toString() +"','" +
[Link]().toString()+"','"+
[Link]().toString() +"','" +
[Link]().toString() + "')");
[Link](getApplicationContext(), "Berhasil",
Toast.LENGTH_LONG).show();
[Link]();
finish();
}
});
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
}
activity_buat_biodata.xml
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BuatBiodata" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Nomor" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="10dp"
android:text="Nama" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="10dp"
android:text="Tanggal Lahir" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginTop="10dp"
android:text="Jenis Kelamin" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText4"
android:layout_below="@+id/editText4"
android:layout_marginTop="10dp"
android:text="Alamat" />
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText5"
android:layout_alignParentBottom="true"
android:background="@drawable/buttonshape"
android:textColor="#FFFFFF"
android:text="Simpan" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:background="@drawable/buttonshape"
android:textColor="#FFFFFF"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/textView4"
android:text="Back" />
</RelativeLayout>
Desain xml untuk insert data
[Link] class ini digunakan untuk melihat record/data
package [Link];
/**
* Created by Gigabyte on 02/11/2016.
*/
import
import
import
import
import
import
import
import
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
public class LihatBiodata extends AppCompatActivity {
protected Cursor cursor;
DataHelper dbHelper;
Button ton2;
TextView text1, text2, text3, text4, text5;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_lihat_biodata);
dbHelper = new DataHelper(this);
text1 = (TextView) findViewById([Link].textView1);
text2 = (TextView) findViewById([Link].textView2);
text3 = (TextView) findViewById([Link].textView3);
text4 = (TextView) findViewById([Link].textView4);
text5 = (TextView) findViewById([Link].textView5);
SQLiteDatabase db = [Link]();
cursor = [Link]("SELECT * FROM biodata WHERE nama = '" +
getIntent().getStringExtra("nama") + "'",null);
[Link]();
if ([Link]()>0)
{
[Link](0);
[Link]([Link](0).toString());
[Link]([Link](1).toString());
[Link]([Link](2).toString());
[Link]([Link](3).toString());
[Link]([Link](4).toString());
}
ton2 = (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
}
activity_lihat_biodata.xml
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LihatBiodata" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="104dp"
android:layout_marginTop="20dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="20dp"
android:text="TextView" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="20dp"
android:text="TextView" />
<TextView
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/TextView03"
android:text="Alamat" />
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/TextView04"
android:text="Jenis Kelamin" />
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/TextView02"
android:text="Tanggal Lahir" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/TextView01"
android:text="Nama" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:text="Nomor" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView05"
android:layout_below="@+id/TextView05"
android:background="@drawable/buttonshape"
android:layout_marginLeft="20dp"
android:textColor="#FFFFFF"
android:layout_marginTop="34dp"
android:text="Back" />
</RelativeLayout>
Desain Lihat Biodata xml
activity_update_biodata.xml
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".UpdateBiodata" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Nomor" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="10dp"
android:text="Nama" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="10dp"
android:text="Tanggal Lahir" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginTop="10dp"
android:text="Jenis Kelamin" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText4"
android:layout_below="@+id/editText4"
android:layout_marginTop="10dp"
android:text="Alamat" />
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText5"
android:layout_alignParentBottom="true"
android:background="@drawable/buttonshape"
android:textColor="#FFFFFF"
android:text="Update" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:layout_marginLeft="20dp"
android:textColor="#FFFFFF"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/textView4"
android:text="Back" />
</RelativeLayout>
Desain Update Biodata
Jika tidak ada error maka program siap untuk dijalankan