android建立自己的数据库访问对象

SQLite在android开发中是用的比较多的,究竟怎样组织代码要好点呢?综合人家优秀的代码,如下去组织代码要好得多:

package com.example.dbtest;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

/**
 * 数据库访问类,单例模式。提供访问数据库的对象
 */
public class DBHelper {
	private final static int db_version = 1 ;
	private final static String tb_name = "test";
	private static DBHelper mInstance = null ;
	private SQLiteOpenHelper mSQLiteDBHelper ;
	private SQLiteDatabase db ;
	private static final String db_name = "dxd";
	private static final String package_name = "com.example.dbtest";
	private static final String db_path = "/data"+ Environment.getDataDirectory().getAbsolutePath() + "/"+ package_name;   // 数据库的保存目录
	
	/**
	 *  建立数据库表的类,dxd.db 下的 test 表。
	 * @author Administrator
	 *
	 */
	private  static class SQLiteHelper extends SQLiteOpenHelper {
		public SQLiteHelper(Context context) {
			super(context, db_name, null, db_version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.e("SQLiteHelper","onCreate");
			String sql = "create table "+tb_name+"( "+
					"id 	Integer PRIMARY KEY  AUTOINCREMENT  NOT NULL ," +
					"number Integer,"+
					"age 	Integer," +
					"name 	VARCHAR" +
					")" ;
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.e("SQLiteHelper","onUpgrade");
			db.execSQL("DROP TABLE IF EXISTS " + tb_name);
			onCreate(db);
		}
	}
	
	private DBHelper(Context c){
		mSQLiteDBHelper = new SQLiteHelper(c);
	}
	
	public static DBHelper getInstance(Context c){
		if(null == mInstance){
			mInstance = new DBHelper(c);
		}
		return mInstance ;
	}
	
	/**
	 * insert
	 * @param num 
	 * @param age
	 * @param name
	 * @return
	 */
	public synchronized boolean insert(int num ,int age,String name){
		Log.e("DBHelper","insert");
		boolean flag = false ;
		try{
			db = mSQLiteDBHelper.getWritableDatabase();
			String sql = "insert into "+tb_name+"(number,age,name) values(?,?,?)";
			db.execSQL(sql, new String[]{String.valueOf(num),String.valueOf(age),name});
			flag = true ;
		}catch(Exception e){
			Log.d("","insert data exception");
		}finally{
			db.close();
		}
		return flag;
	}
	
	/**
	 * delete
	 * @param num
	 * @return
	 */
	public synchronized boolean deleteByNum(int num){
		boolean flag = false ;
		try{
			db = mSQLiteDBHelper.getWritableDatabase();
			String sql = "delete from " + tb_name + "where number = ?";
			db.execSQL(sql, new String[]{String.valueOf(num)});
			flag = true ;
		}catch(Exception e){
			Log.d("","deleteByNum exception");
		}finally{
			db.close();
		}
		return flag ;
	}
	
	/**
	 * select by number
	 * @param num
	 * @return
	 */
	public synchronized boolean selectByNum(int num){
		boolean flag = false ;
		try{
			db = mSQLiteDBHelper.getWritableDatabase();
			String sql = "select from " + tb_name + "where number = ?";
			Cursor c = null;
			c = db.rawQuery(sql, new String[]{String.valueOf(num)});
			if(c != null){
				if(c.moveToFirst()){
					do{
						int number = c.getInt(0);
						int age = c.getInt(1);
						String name = c.getString(2);
						Log.e("","number = " + number + "  age = " + age +"   name = " + name);
					}while(c.moveToNext());
				}
			}
	        flag = true ;
		}catch(Exception e){
			Log.d("","selectByNum data exception");
		}finally{
			db.close();
		}
		return flag ;
	}
	
	/**
	 * select all
	 * @return
	 */
	public synchronized boolean selectAll(){
		Log.e("DBHelper","selectAll");
		boolean flag = false ;
		try{
			db = mSQLiteDBHelper.getWritableDatabase();
			String sql = "select * from " + tb_name;
			Cursor c = null;
			c = db.rawQuery(sql, null);
			if(c != null){
				if(c.moveToFirst()){
					do{
						int pk = c.getInt(0);
						int number = c.getInt(1);
						int age = c.getInt(2);
						String name = c.getString(3);
						Log.e("","pk = " + pk + "  number = " + number + "  age = " + age +"   name = " + name);
					}while(c.moveToNext());
				}
			}
			flag = true ;
		}catch(Exception e){
			Log.d("","selectAll data exception");
		}finally{
			db.close();
		}
		return flag ;
	}
}
SQLiteOpenHelper对象作为静态类在DBHelper加载的时候就会去创建数据库,这2个重载的方法是SQLiteOpenHelper类的子类必须要实现的接口,主要是负责数据库的创建和更新,将我们自己定义的数据库访问类DBHelper设计成单例模式,在用的时候只需要getInstance即可,通过同步操作不会引起混乱。这里只是写出了插入和查询的方法,如果需要可以增加很多访问数据库的方法,这就要看项目的需要了。

你可能感兴趣的:(数据库)