menggunakan database SQLite pada android adalah salah satu metode penyimpanan yang data yang dapat dilakukan dengan mudah, berikut source code nya:
MainActivity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginLeft="60dp" android:layout_marginTop="23dp" android:onClick="simpan" android:text="simpan" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/nomorId" android:layout_marginTop="16dp" android:onClick="load" android:text="load" /> <EditText android:id="@+id/nomorId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/button1" android:layout_marginTop="68dp" android:ems="10" android:inputType="number" /> <EditText android:id="@+id/hasil" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/nomorId" android:layout_below="@+id/button2" android:ems="10" /> </RelativeLayout>
MainActivity.java:
package com.example.cobasqlt; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText text1, text2,text3; Button btn1, btn2; MySQLiteHelper db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1=(EditText) findViewById(R.id.editText1); text2=(EditText) findViewById(R.id.nomorId); text3=(EditText) findViewById(R.id.hasil); db=new MySQLiteHelper(this); } public void simpan(View view){ db.menambahkan(text1.getText().toString()); } public void load(View view){ String hasil=db.mangambil(Integer.parseInt(text2.getText().toString())); //merubah input id type string mejadi int text3.setText(hasil+""); } @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; } }
MySQLiteHelper.java:
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class MySQLiteHelper extends SQLiteOpenHelper{ private static final int VERSI_DATABASE=1; private static final String NAMA_DATABASE="DatabaseSatu"; //namadatabase public MySQLiteHelper(Context context) { super(context, NAMA_DATABASE, null, VERSI_DATABASE); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { String MEMBUAT_TABLE_TABEL="CREATE TABLE tabelsatu("+"id INTEGER PRIMARY KEY AUTOINCREMENT,"+ "masukan TEXT)"; db.execSQL(MEMBUAT_TABLE_TABEL); // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXIST tabelsatu"); // jika terdapat tabel tabelsatu maka dihapus // TODO Auto-generated method stub this.onCreate(db);// merefresh setelah dihapus } public void menambahkan(String input){ SQLiteDatabase db = this.getWritableDatabase(); // 2. create ContentValues to add key "column"/value ContentValues values = new ContentValues(); values.put("masukan", input); // get title // 3. insert db.insert("tabelsatu", // table null, //nullColumnHack values); // key/value -> keys = column names/ values = column values // 4. close db.close(); } public String mangambil(int id){ // 1. get reference to readable DB SQLiteDatabase db = this.getReadableDatabase(); String[] namaKolom={"masukan"}; // 2. build query Cursor cursor = db.query("tabelsatu", // a. table namaKolom, // b. column names harus brupa array STRING " id = ?", // c. selections new String[] { String.valueOf(id) }, // d. selections args null, // e. group by null, // f. having null, // g. order by null); // h. limit if (cursor != null) cursor.moveToFirst(); return cursor.getString(0); // mengabil column ke 0 } }
0 komentar:
Post a Comment