SQLiteDatabase 进一步整合

前几篇关于"SQLiteDatabase" 时候   辅助类"SQLiteOpenHelper"的地位

 

今天在把 SQLiteDatabase 与 SQLiteOpenHelper 试图整合 有点想法 与大家分享分享

 

 

[思路]

1. SQLiteDatabase  用于数据库操作 所以必须返回一个句柄

2. SQLiteOpenHelper 用于协助SQLiteDatabase 

 

 

 

[代码 步骤]

1. 定义 SQLiteHandler 的内部类 DBHelper 定义如下:

public class DBHelper extends SQLiteOpenHelper {

		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL("CREATE TABLE IF NOT EXISTS " 
	                + TB_NAME + " (" 
	        		+ ID + " INTEGER PRIMARY KEY," 
	        	    + COUNTRY + " VARCHAR,"
	        	    + CODE + " INTEGER)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
	        onCreate(db);
		}
		
	}

 

 

 

2. 定义 SQLiteHandler 构造函数 及 获取 SQLiteDatabase 句柄 的 函数 如下:

DBHelper dbHelper;
	static SQLiteDatabase sqliteInstance;
	
	Context context;
	
	public SQLiteHandler(Context c,String name,int version){
		context = c;
		
		dbHelper = new DBHelper(context,name,null,version);
		
		sqliteInstance = dbHelper.getWritableDatabase();
	}
	
	public static SQLiteDatabase GetSQLiteHandler(){
		return sqliteInstance;
	}

 

 

3. 如何使用

public class SQLiteHandlerUsage extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SQLiteHandler handler = new SQLiteHandler(this,"countrycode.db",1);
        
        SQLiteDatabase db = handler.GetSQLiteHandler();
        
        ContentValues values = new ContentValues();
        values.put(SQLiteHandler.COUNTRY, "中国");
        values.put(SQLiteHandler.CODE, 86);
        db.insert(SQLiteHandler.TB_NAME,SQLiteHandler.ID, values);
        
        ContentValues values2 = new ContentValues();
        values2.put(SQLiteHandler.COUNTRY, "台湾");
        values2.put(SQLiteHandler.CODE, 186);
        db.insert(SQLiteHandler.TB_NAME,SQLiteHandler.ID, values2);
        
        
        db.insert(SQLiteHandler.TB_NAME,SQLiteHandler.ID,null);
        values.clear();
        values.put(SQLiteHandler.COUNTRY, "意大利");
        values.put(SQLiteHandler.CODE, 39);
        db.update(SQLiteHandler.TB_NAME, values,SQLiteHandler.ID + " = 2",null);
        
        db.execSQL("INSERT INTO "
                + SQLiteHandler.TB_NAME + "("
                + SQLiteHandler.COUNTRY + ","
                + SQLiteHandler.CODE + ") VALUES "
                + "('洪都拉斯',504)");
        
        
        Cursor c = db.query(SQLiteHandler.TB_NAME,null,null,null,null,null,
        		SQLiteHandler.CODE+" DESC");
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2, 
                c,
                new String[] {SQLiteHandler.COUNTRY,SQLiteHandler.CODE}, 
                new int[] {android.R.id.text1,android.R.id.text2});
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        
        
        this.setListAdapter(adapter);
    }
}

 

 

 

4. 运行效果 略过

 SQLiteDatabase 进一步整合

 

你可能感兴趣的:(C++,c,android,C#)