membuat crud databaseSqlite


Cara Membuat CRUD Database SQlite di android Studio 


Assalamualikum warahmatullahi wabarakatu..
Pada blog saya kali ini saya akan membagikan tutorial membuat Crud database SQlite di android studio, oke langsung saja .

  1. Jalankan aplikasi android studio, buat project baru klik file->new->new project.
  2.  Buat class baru dan berinama DataHelper, klik knan pada java new->java class.
  3. Pda DataHelper masukkan scrip berikut: 
    package com.example.mykoperasi;
    import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;
    public class DataHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "simpanan.db";    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 simpanan(no integer primary key, nama text null,
            nominal text null, tanggal text null, alamat text null);";        Log.d("Data", "onCreate: " + sql);        db.execSQL(sql);        sql = "INSERT INTO biodata (no, nama, nominal, tanggal, alamat) 
            VALUES ('1001', 'Fadlun', '2.000.000', '2019-07-1','Panggulo');";        db.execSQL(sql);
        }
    
        @Override    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
        }
    }
    
  4. Pada activity.xml kita masukkan script dibawah ini
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".Akun"    android:orientation="vertical"    android:padding="10dp"    android:background="@drawable/asa">
        <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/button1"        style="?android:attr/borderlessButtonStyle"        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:alpha="0.4"        android:layout_centerVertical="true" android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"/>
    </RelativeLayout>
  5. actifity.java
    package com.example.mykoperasi;
    import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.PopupMenu;import android.widget.Toast;
    import java.time.Period;import java.util.ArrayList;import java.util.List;
    public class Akun extends Activity {
    
        String[] daftar;    ListView ListView01;    Menu menu;    protected Cursor cursor;    DataHelper dbcenter;    public static Akun ma;
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        setContentView(R.layout.activity_akun);
            Button btn=(Button)findViewById(R.id.button2);
            btn.setOnClickListener(new View.OnClickListener() {
    
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                Intent inte = new Intent(Akun.this, MainActivity.class);                startActivity(inte);            }
            });
    
            ma = this;        dbcenter = new DataHelper(this);        RefreshList();    }
    
        public void RefreshList(){
            SQLiteDatabase db = dbcenter.getReadableDatabase();        cursor = db.rawQuery("SELECT * FROM simpanan",null);        daftar = new String[cursor.getCount()];        cursor.moveToFirst();        for (int cc=0; cc < cursor.getCount(); cc++){
                cursor.moveToPosition(cc);            daftar[cc] = cursor.getString(1).toString();        }
            ListView01 = (ListView)findViewById(R.id.listView1);        ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));        ListView01.setSelected(true);        ListView01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
    
                public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
                    final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();                final CharSequence[] dialogitem = {"Lihat simpanan", "Update simpanan", "Hapus simpanan"};                AlertDialog.Builder builder = new AlertDialog.Builder(Akun.this);                builder.setTitle("Pilihan");                builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            switch(item){
                                case 0 :
                                    Intent i = new Intent(getApplicationContext(), LihatData.class);                                i.putExtra("nama", selection);                                startActivity(i);                                break;                            case 1 :
                                    Intent in = new Intent(getApplicationContext(), UpdateData.class);                                in.putExtra("nama", selection);                                startActivity(in);                                break;                            case 2 :
                                    SQLiteDatabase db = dbcenter.getWritableDatabase();                                db.execSQL("delete from simpanan where nama = '"+selection+"'");                                RefreshList();                                break;                        }
                        }
                    });                builder.create().show();            }});        ((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();    }
    
        @Override    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }
    
    }
  6. Selanjutnya buat 3 activity baru berinama buat data, update data, dan lihat data.
  7. Pada activity_buat_data.xml masukkan script seperti di bawah ini
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:background="@drawable/asa">
        <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="Nominal Simpanan" />
        <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="Tanggal" />
        <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="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/editText5"        android:layout_alignParentBottom="true"        android:text="Simpan" />
        <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_toRightOf="@+id/textView4"        android:text="Back" />
    </RelativeLayout>
  8. buat_data.java
    package com.example.mykoperasi;
    import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;
    public class MainActivity extends Activity {
        public static Akun ma;    protected Cursor cursor;    DataHelper dbHelper;    Button ton1, ton2;    EditText text1, text2, text3, text4, text5;
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        dbHelper = new DataHelper(this);        text1 = (EditText) findViewById(R.id.editText1);        text2 = (EditText) findViewById(R.id.editText2);        text3 = (EditText) findViewById(R.id.editText3);        text4 = (EditText) findViewById(R.id.editText4);        text5 = (EditText) findViewById(R.id.editText5);        ton1 = (Button) findViewById(R.id.button1);        ton2 = (Button) findViewById(R.id.button2);
            ton1.setOnClickListener(new View.OnClickListener() {
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                SQLiteDatabase db = dbHelper.getWritableDatabase();                db.execSQL("insert into simpanan(no, nama, nominal, tanggal, alamat) values('" +
                            text1.getText().toString()+"','"+
                            text2.getText().toString() +"','" +
                            text3.getText().toString()+"','"+
                            text4.getText().toString() +"','" +
                            text5.getText().toString() + "')");                Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();                MainActivity.ma.RefreshList();                finish();            }
            });        ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                finish();            }
            });    }
    
        @Override    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }
    
    }
  9. Pada activity_update_data.xml masukkan script seperti di bawah ini
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".UpdateData">
        <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="Nominal pinjaman" />
        <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="Tanggal" />
        <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="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/editText5"        android:layout_alignParentBottom="true"        android:text="Update" />
        <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_toRightOf="@+id/textView4"        android:text="Back" />
    </RelativeLayout>
  10. update_data.java
    package com.example.mykoperasi;
    import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;
    public class UpdateData extends AppCompatActivity {
        protected Cursor cursor;    DataHelper dbHelper;    Button ton1, ton2;    EditText text1, text2, text3, text4, text5;
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        setContentView(R.layout.activity_update_data);
            dbHelper = new DataHelper(this);        text1 = (EditText) findViewById(R.id.editText1);        text2 = (EditText) findViewById(R.id.editText2);        text3 = (EditText) findViewById(R.id.editText3);        text4 = (EditText) findViewById(R.id.editText4);        text5 = (EditText) findViewById(R.id.editText5);        SQLiteDatabase db = dbHelper.getReadableDatabase();        cursor = db.rawQuery("SELECT * FROM simpanan WHERE nama = '" +
                    getIntent().getStringExtra("nama") + "'",null);        cursor.moveToFirst();        if (cursor.getCount()>0)
            {
                cursor.moveToPosition(0);            text1.setText(cursor.getString(0).toString());            text2.setText(cursor.getString(1).toString());            text3.setText(cursor.getString(2).toString());            text4.setText(cursor.getString(3).toString());            text5.setText(cursor.getString(4).toString());        }
            ton1 = (Button) findViewById(R.id.button1);        ton2 = (Button) findViewById(R.id.button2);        // daftarkan even onClick pada btnSimpan        ton1.setOnClickListener(new View.OnClickListener() {
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                SQLiteDatabase db = dbHelper.getWritableDatabase();                db.execSQL("update simpanan set nama='"+
                            text2.getText().toString() +"', nominal='" +
                            text3.getText().toString()+"', tanggal='"+
                            text4.getText().toString() +"', alamat='" +
                            text5.getText().toString() + "' where no='" +
                            text1.getText().toString()+"'");                Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();                MainActivity.ma.RefreshList();                finish();            }
            });        ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                finish();            }
            });    }
    
        @Override    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }
    
    }
    
  11. Pada activity_lihat_data.xml masukkan script seperti di bawah ini
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".LihatData">
        <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="Tanggal" />
        <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="Nominal pinjaman" />
        <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="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/TextView05"        android:layout_below="@+id/TextView05"        android:layout_marginTop="34dp"        android:text="Back" />
    </RelativeLayout>
  12. lihat_data.java
    package com.example.mykoperasi;
    import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.TextView;
    public class LihatData extends AppCompatActivity {
        protected Cursor cursor;    DataHelper dbHelper;    Button ton2;    TextView text1, text2, text3, text4, text5;
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        setContentView(R.layout.activity_lihat_data);
            dbHelper = new DataHelper(this);        text1 = (TextView) findViewById(R.id.textView1);        text2 = (TextView) findViewById(R.id.textView2);        text3 = (TextView) findViewById(R.id.textView3);        text4 = (TextView) findViewById(R.id.textView4);        text5 = (TextView) findViewById(R.id.textView5);        SQLiteDatabase db = dbHelper.getReadableDatabase();        cursor = db.rawQuery("SELECT * FROM simpanan WHERE nama = '" +
                    getIntent().getStringExtra("nama") + "'",null);        cursor.moveToFirst();        if (cursor.getCount()>0)
            {
                cursor.moveToPosition(0);            text1.setText(cursor.getString(0).toString());            text2.setText(cursor.getString(1).toString());            text3.setText(cursor.getString(2).toString());            text4.setText(cursor.getString(3).toString());            text5.setText(cursor.getString(4).toString());        }
            ton2 = (Button) findViewById(R.id.button1);        ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override            public void onClick(View arg0) {
                    // TODO Auto-generated method stub                finish();            }
            });    }
    
        @Override    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }
    
    }
    
  13. sudah selesai tinggal di running saja, selamat mencoba

Comments

Popular posts from this blog

versie versi android