依前文的程式,繼續增加其它功能,使其擁有最基本的資料庫管理功能
(1)新增資料
(2)修改資料
(3)刪除資料
(4)查詢上一筆資料
(5)查詢下一筆資料
注意,本程式單純範例,因此完全不考慮requery及cursor move超出陣列空間的問題。實際撰寫程式時,需調整程式避免錯誤發生。
MyContentProvider.java 如下:
public class MyContentProvider extends ContentProvider {
private MyOpenHelper myOpenHelper;
private SQLiteDatabase myDB;
public static final String AUTHORITY = "com.gill.test.authorities";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/MyTable");
private static final String[] MYCOLUMN = new String[] { "mycolumn" };
@Override
public boolean onCreate() {
myOpenHelper = new MyOpenHelper(getContext());
myDB = myOpenHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase myDatabase = myOpenHelper.getWritableDatabase();
Cursor c = myDatabase.query(uri.getLastPathSegment(), MYCOLUMN, null,
null, null, null, null);
return c;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowId = -1;
//第二個參數隨便是該table內的任一個欄位,反正insert就是一整筆下去新增,系統會自動配對欄位和values
rowId = myDB.insert(uri.getLastPathSegment(), "mycolumn", values);
Uri newUri = Uri.withAppendedPath(CONTENT_URI, "" + rowId);
return newUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int del_count = myDB.delete(uri.getLastPathSegment(), selection,
selectionArgs);
return del_count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int update_count = myDB.update(uri.getLastPathSegment(), values, selection, selectionArgs);
return update_count;
}
}
Main.java如下:
public class Main extends Activity {
private ContentResolver mResolver;
private Cursor myCursor;
private TextView txt;
private EditText edit;
// AUTHORITY要看該Provider的AndroidManifest.xml中的定義
public static final String AUTHORITY = "com.gill.test.authorities";
// URI = AUTHORITY再加table name
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/MyTable");
private static final String[] MYCOLUMN = new String[] {"mycolumn" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mResolver = getContentResolver();
// 透過getContentResolver由系統依據uri,決定要執行那一個ContentProvider
// 所以這裡不用直接呼叫MyDataBaseProvider也可以取得資料
myCursor = mResolver.query(CONTENT_URI, MYCOLUMN, null,
null, null);
myCursor.moveToFirst(); //一定要寫,不然會出錯
txt = (TextView) findViewById(R.id.txt);
txt.setText(myCursor.getString(0));
edit = (EditText) findViewById(R.id.edit);
Button insert_btn = (Button) findViewById(R.id.insert);
Button update_btn = (Button) findViewById(R.id.update);
Button delete_btn = (Button) findViewById(R.id.delete);
Button previous_btn = (Button) findViewById(R.id.previous);
Button next_btn = (Button) findViewById(R.id.next);
//新增
OnClickListener insert_lis = new OnClickListener(){
public void onClick(View v) {
ContentValues values = new ContentValues();
//如果table有很多欄位,就在這裡把所有的欄位利用values.put補齊
values.put("mycolumn", edit.getText().toString());
mResolver.insert(CONTENT_URI, values );
txt.setText(myCursor.getString(0));
}
};
insert_btn.setOnClickListener(insert_lis);
//修改
OnClickListener update_lis = new OnClickListener(){
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("mycolumn", edit.getText().toString());
String[] args = new String[] { txt.getText().toString() };
mResolver.update(CONTENT_URI, values, "mycolumn=?", args);
txt.setText(myCursor.getString(0));
}
};
update_btn.setOnClickListener(update_lis);
//刪除
OnClickListener delete_lis = new OnClickListener(){
public void onClick(View v) {
String[] args = new String[] { txt.getText().toString()};
mResolver.delete(CONTENT_URI, "mycolumn=?", args);
txt.setText(myCursor.getString(0));
}
};
delete_btn.setOnClickListener(delete_lis);
//上一筆
OnClickListener previous_lis = new OnClickListener(){
public void onClick(View v) {
myCursor.moveToPrevious();
txt.setText(myCursor.getString(0));
}
};
previous_btn.setOnClickListener(previous_lis);
//下一筆
OnClickListener next_lis = new OnClickListener(){
public void onClick(View v) {
myCursor.moveToNext();
txt.setText(myCursor.getString(0));
}
};
next_btn.setOnClickListener(next_lis);
}
}
程式畫面如下:
不好意思 請問一下大大 我照著 教學實作~
回覆刪除插入跟刪除 我用命令提示字元看 都有成功
上一個跟下一個按鈕 在手機中都會出現強制終止的畫面
該怎麼解決呢???