通过这个小例子大致了解了手机数据库的一些功能,见主类
- package com.android.sucre;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteCursor;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- /**
- * 数据库简单应用
- * @author qiaolei
- *
- */
- public class EX05_05_SQLite extends Activity {
- private ToDoDB myToDoDB;
- private Cursor myCursor;
- private EditText myEditText;
- private ListView myListView;
- private int _id;
- protected final static int MENU_ADD = Menu.FIRST;
- protected final static int MENU_EDIT = Menu.FIRST + 1;
- protected final static int MENU_DELETE = Menu.FIRST + 2;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myEditText = (EditText)findViewById(R.id.myEditText);
- myListView = (ListView)findViewById(R.id.myListView);
- myToDoDB = new ToDoDB(this);
- myCursor = myToDoDB.select();//取得database中的数据
- //R.layout.list为layout文件夹下的list文件
- //R.id.listView指向list文件的ID
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, myCursor, new String[]{ToDoDB.FIELD_TEXT}, new int[]{R.id.listView});
- myListView.setAdapter(adapter);
- myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- myCursor.moveToPosition(arg2);//将myCursor移到所单击的值,这个值是数据库中的行标是固定不变的
- _id = myCursor.getInt(0);//取得第0列字段_id的值,这个值相当于数据库的主键处于自增状态,相对于整个数据库而言,这个值是实际数据库中字段的真实值
- Log.d("arg2:_id", arg2+":"+_id);
- myEditText.setText(myCursor.getString(1));//取得第1列字段的值
- }
- });
- myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- SQLiteCursor sc = (SQLiteCursor)arg0.getSelectedItem();
- _id = sc.getInt(0);
- myEditText.setText(sc.getString(1));
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(Menu.NONE, MENU_ADD, 0, R.string.create);
- menu.add(Menu.NONE, MENU_EDIT, 0, R.string.edit);
- menu.add(Menu.NONE, MENU_DELETE, 0, R.string.delete);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- super.onOptionsItemSelected(item);
- switch (item.getItemId()) {
- case MENU_ADD:
- this.addToDo();
- break;
- case MENU_EDIT:
- this.editToDo();
- break;
- case MENU_DELETE:
- this.deleteToDo();
- break;
- }
- return true;
- }
- private void addToDo(){
- if(!"".equals(myEditText.getText().toString())){
- myToDoDB.insert(myEditText.getText().toString());
- myCursor.requery();
- myListView.invalidateViews();
- myEditText.setText("");
- _id = 0;
- }
- }
- private void editToDo(){
- if(!"".equals(myEditText.getText().toString())){
- myToDoDB.update(_id, myEditText.getText().toString());
- myCursor.requery();//更新游标
- myListView.invalidateViews();//更新ListView内容
- myEditText.setText("");
- _id = 0;
- }
- }
- private void deleteToDo(){
- if(_id!=0){
- myToDoDB.delete(_id);
- myCursor.requery();
- myListView.invalidateViews();
- myEditText.setText("");
- _id = 0;
- }
- }
- }
- package com.android.sucre;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
- /**
- * 创建数据库
- * @author qiaolei
- *
- */
- public class ToDoDB extends SQLiteOpenHelper{
- private final static String DATABASE_NAME= "todo_db";
- private final static int DATABASE_VERSION = 1;
- private final static String TABLE_NAME = "todo_table";
- public final static String FIELD_id = "_id";
- public final static String FIELD_TEXT = "todo_text";
- public ToDoDB(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- //创建的表有两列,FIELD_ID为主键自动增长
- String sql = "create table "+TABLE_NAME+" ("+FIELD_id+" integer primary key autoincrement,"+FIELD_TEXT+" text)";
- db.execSQL(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- String sql = "drop table if exists "+TABLE_NAME+"";
- db.execSQL(sql);
- onCreate(db);
- }
- public Cursor select(){
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
- return cursor;
- }
- public long insert(String text){
- SQLiteDatabase db = this.getReadableDatabase();
- ContentValues cv = new ContentValues();
- cv.put(FIELD_TEXT, text);
- long row = db.insert(TABLE_NAME, null, cv);
- return row;
- }
- public void delete(int id){
- SQLiteDatabase db = this.getReadableDatabase();
- String where = FIELD_id + "=?";
- String[] whereValue = {Integer.toString(id)};
- db.delete(TABLE_NAME, where, whereValue);
- }
- public void update(int id,String text){
- SQLiteDatabase db = this.getReadableDatabase();
- String where = FIELD_id + "=?";
- String[] whereValue = {Integer.toString(id)};
- ContentValues cv = new ContentValues();
- cv.put(FIELD_TEXT, text);
- db.update(TABLE_NAME, cv, where, whereValue);
- }
- }
其它配置文件见附件
转载于:https://blog.51cto.com/sucre/765101