android数据持久化之SQLite

  • 内容
  • 评论
  • 相关

简单讲解SQLite关系数据库存储数据的用法
创建DBAdapter辅助类,封装访问数据库的操作

package com.example.mydatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	static final String KEY_ROWID = "_id";
	static final String KEY_NAME = "name";
	static final String KEY_EMAIL = "email";
	static final String TAG = "DBAdapter";
	
	static final String DATABASE_NAME = "MyDB";
	static final String DATABASE_TABLE = "contacts";
	static final int DATABASE_VERSION = 2;
	
	static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement,"
			+ "name text not null, email text not null)";
	final Context context;
	DatabaseHelper DBHelper;
	SQLiteDatabase db;
	
	public DBAdapter(Context ctx){
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper{

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try{
				db.execSQL(DATABASE_CREATE);
			}catch (SQLException e){
				e.printStackTrace();
			}
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
			
		}
		
	}
	
	public DBAdapter open() throws SQLException{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		DBHelper.close();
	}
	
	public long insertContact(String name, String email){
		ContentValues initiaValues = new ContentValues();
		initiaValues.put(KEY_NAME, name);
		initiaValues.put(KEY_EMAIL, email);
		return db.insert(DATABASE_TABLE, null, initiaValues);
	}
	
	public boolean deleteContact(long rowId){
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	public Cursor getAllContacts(){
		return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL}, null, null, null, null, null);
	}
	
	public Cursor getContact(long rowId) throws SQLException{
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if(mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean updateContact(long rowId, String name, String email){
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_EMAIL, email);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}

主程序文件

package com.example.mydatabase;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DBAdapter db = new DBAdapter(this);
		
//		db.open();
//		long id = db.insertContact("gaoxueping", "gaoxueping@china.com");
//		id = db.insertContact("andy", "andy@china.com");
//		db.close();
		
		db.open();
		Cursor c = db.getAllContacts();
		if(c.moveToFirst()){
			do{
				DisplayContact(c);
			} while (c.moveToNext());
		}
	}

	@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;
	}
	
	public void DisplayContact(Cursor c){
		Toast.makeText(this, 
				"id: " + c.getString(0) + "\n"
				+"name: " + c.getString(1) + "\n"
				+"email: " +c.getString(2), 
				Toast.LENGTH_LONG).show();
	}

}

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注