Insert Queries
By Fanxtea | 1 Januari 2015Tutorial ini akan menunjukkan bagaimana user menggunakan insert queries untuk memasukkan data ke dalam pangkalan data.
- Android Layout Buka fail res/layout/activity_main.xml, tambah satu butang dan dua komponen Edit Text, satu untuk nama dan satu lagi alamat, untuk disimpan ke dalam pangkalan data.
- Kod Fail: MainActivity.java
- Demo Run aplikasi.
Fail: res/layout/activity_main.xml
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/textViewName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="@+id/textViewAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextAddress" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/btnAdd" />
</LinearLayout>
package com.fanxtea.helloworld;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText editTextName;
private EditText editTextAdd;
private Button btnAdd;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDatabase();
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAdd = (EditText) findViewById(R.id.editTextAddress);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
}
protected void createDatabase(){
db=openOrCreateDatabase("PersonDB.db", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");
}
protected void insertIntoDB(){
String name = editTextName.getText().toString().trim();
String add = editTextAdd.getText().toString().trim();
if(name.equals("") || add.equals("")){
Toast.makeText(getApplicationContext(),"Please fill all fields", Toast.LENGTH_LONG).show();
return;
}
String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";
db.execSQL(query);
Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
if(v == btnAdd){
insertIntoDB();finish();
startActivity(getIntent());
}
}
}
Sila masukkan nama dan alamat.
Data telah disimpan.
P/s: Untuk melihat data yang disimpan tadi, boleh pergi ke sini.