เพื่อให้ง่ายในการเรียน เราจะเริ่มใหม่ตั้งแต่ต้น
1. เลือกเมนู Files > New > Android Project
2. พิมพ์ว่า My App แล้วกด Next ไปเรื่อยๆ แล้วกด Finish
3. ไฟล์ activity_main.xml ให้ใส่ Code นี้
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Connect SQLite" />
<EditText
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/txt_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age (Number Only)" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="btn_insert_clicked"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="btn_update_clicked"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:onClick="btn_delete_clicked" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txt_res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
</LinearLayout>
4. ไฟล์ MainActivity.java ให้ใส่ Code นี้
package com.example.myapp;
import java.util.Locale;
import android.app.Activity;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText txt_name;
EditText txt_age;
TextView txt_res;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_name = (EditText) findViewById(R.id.txt_name);
txt_age = (EditText) findViewById(R.id.txt_age);
txt_res = (TextView) findViewById(R.id.txt_res);
db = openOrCreateDatabase( "myapp.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); // สร้าง Database ชื่อ test1.db
db.setVersion(1);
db.setLocale(Locale.getDefault());
try {
String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS person (" // ถ้าไม่มี table person ให้ทำการสร้าง
+ "id INTEGER primary key AUTOINCREMENT," // ฟิลด์ id ให้เป็น PK
+ "name TEXT," // ฟิลด์ name ชนิด Text
+ "age INTEGER);"; // ฟิลด์ age ชนิด Integer
db.execSQL(CREATE_TABLE_CONTAIN);
} catch (Exception e) {}
showdata();
}
public void btn_insert_clicked(View v) {
String name = txt_name.getText().toString(); // รับค่าจาก txt_name
String str_age = txt_age.getText().toString();
if (name.equals("") && str_age.equals("")) { return; }
Integer age = Integer.valueOf(str_age);
String sql ="insert into person values(null,'" + name + "'," + age + ")";
try{
db.execSQL(sql);
}catch(Exception e){
}finally{
//txt_name.setText(""); txt_age.setText("");
showdata();
}
}
public void btn_update_clicked(View v) {
String sql = "update person set age=0 where id=1";
db.execSQL(sql);
showdata();
}
public void btn_delete_clicked(View v) {
String id = txt_name.getText().toString(); // รับค่าจาก txt_name
String sql = "delete from person where id = '" + id + "'";
db.execSQL(sql);
showdata();
}
public void showdata() { // ดึงข้อมูลมาแสดง
txt_res.setText("");
SQLiteCursor cur = (SQLiteCursor) db.rawQuery("select * from person", null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
txt_res.append("id :"+cur.getString(0)+" ชื่อ :"+cur.getString(1)+" อายุ :"+cur.getString(2)+"\n");
cur.moveToNext();
}
cur.close();
}
protected void onDestroy() {
super.onDestroy();
db.close();
}
}
อ้างอิง : บทความนี้ศึกษาจาก
http://android4health.wordpress.com/2012/06/23/sqlite_manage/
ขอบคุณเจ้าของบทความที่เขียนเข้าใจง่ายครับ
22 พ.ค. 57