接續前面的二篇文章"[android SQLite] SQLite 建立資料庫/新增table / 刪除 table"、"[android SQLite] SQLite 新增和查詢資料",再來要做刪除與修改的功能。
在寫程式時,刪除和修改要特別小心,一定要指定一個唯一的值給他Filter,不然一刪就把所有的資料刪掉了,修改也一樣,所以給他一個唯一的值做Filter,這是非常重要的。
將專案再Run一次,這時會看到如下二筆資料:(其id分別為1和2,共二筆資料),每次執行專案都會新增一筆資料,不會重新從空的DB開始。
若要從空的DB開始,請看這篇"[android SQLite] SQLite 簡介",找到xxx.db,將其刪除,則專案執行會重新建立新的DB,就會從空的DB開始囉~
刪除和修改,將主程式改為如下:
這裡會先新增一筆id為3的資料,再刪除id為1的資料,再修改id為2的資料,然後顯示資料。
package com.example.test;
import static android.provider.BaseColumns._ID;
//呼叫DBHelper類別定義的常數
import static com.example.test.DBHelper.EMAIL;
import static com.example.test.DBHelper.NAME;
import static com.example.test.DBHelper.TABLE_NAME;
import static com.example.test.DBHelper.TEL;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private DBHelper dbhelper = null;
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DBHelper(this);
dbhelper.close();
add(); //每次應用程式執行時,會新增一筆david的資料
result = (TextView) findViewById(R.id.textView2);
del(); //刪除資料
update(); //修改資料
show(); //顯示資料內容
}
//刪除資料
private void del(){
String id = "1"; //刪除id為1的資料
SQLiteDatabase db = dbhelper.getWritableDatabase();
db.delete(TABLE_NAME, _ID + "=" + id, null);
}
//修改資料
private void update(){
String id = "2"; //修改id為2的資料
ContentValues values = new ContentValues();
values.put(NAME, "Eva");
values.put(TEL, "0425971489");
values.put(EMAIL, "Eva@kimo.com");
SQLiteDatabase db = dbhelper.getWritableDatabase();
db.update(TABLE_NAME, values, _ID + "=" + id, null);
}
//新增資料
private void add(){
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, "david");
values.put(TEL, "03-21451745");
values.put(EMAIL, "david@yahoo.com");
db.insert(TABLE_NAME, null, values);
}
//顯示資料
private void show(){
Cursor cursor = getCursor();
StringBuilder resultData = new StringBuilder("RESULT: \n");
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
String tel = cursor.getString(2);
String email = cursor.getString(3);
resultData.append(id).append(": ");
resultData.append(name).append(", ");
resultData.append(tel).append(", ");
resultData.append(email).append(", ");
resultData.append("\n");
}
result.setText(resultData);
}
@SuppressWarnings("deprecation")
private Cursor getCursor(){
SQLiteDatabase db = dbhelper.getReadableDatabase();
String[] columns = {_ID, NAME, TEL, EMAIL};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
startManagingCursor(cursor);
return cursor;
}
@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;
}
}
執行結果:
新增一筆id為3的資料,再刪除id為1的資料,再修改id為2的資料。
留言列表