Advertisement
Cyber34

Untitled

Sep 14th, 2012
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package com.pixign.healthy.lady.dbs;
  2.  
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  8. import android.util.Log;
  9.  
  10. public abstract class DBA_Base {
  11.  
  12.     private static final String MYDATABASE_NAME = "hl_db";
  13.     private static final int MYDATABASE_VERSION = 1;
  14.    
  15.     protected SQLiteHelper sqLiteHelper;
  16.     protected SQLiteDatabase sqLiteDatabase;
  17.     protected Context context;
  18.    
  19.     public DBA_Base(Context c){
  20.         context = c;
  21.     }
  22.    
  23.     public DBA_Base openToRead() throws android.database.SQLException {
  24.         sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  25.         sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  26.         sqLiteHelper.forceIt(sqLiteDatabase);
  27.         return this;
  28.     }
  29.     public DBA_Base openToWrite() throws android.database.SQLException {
  30.         sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  31.        
  32.         sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  33.         return this;
  34.     }
  35.    
  36.     public void close(){
  37.         sqLiteHelper.close();
  38.     }
  39.    
  40.     public void deleteAll(){
  41.         sqLiteDatabase.delete(getTableName(), null, null);
  42.     }
  43.    
  44.     public Cursor queryAll() {
  45.         Cursor cursor = sqLiteDatabase.query(getTableName(), getColNames(),
  46.           null, null, null, null, null);
  47.         return cursor;
  48.     }
  49.    
  50.     public Cursor queryById(int id){
  51.         String c1 = getColNames()[0];
  52.         String[] columns = new String[]{c1};
  53.         Cursor cursor = sqLiteDatabase.query(getTableName(), columns,
  54.                 c1 + " = " + id, null, null, null, null);
  55.         return cursor;
  56.     }
  57.    
  58.     public class SQLiteHelper extends SQLiteOpenHelper {
  59.         public SQLiteHelper(Context context, String name,
  60.           CursorFactory factory, int version) {
  61.             super(context, name, factory, version);
  62.         }
  63.         @Override
  64.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  65.         @Override
  66.         public void onCreate(SQLiteDatabase db) {
  67.             forceIt(db);
  68.            
  69.         }
  70.         public void forceIt(SQLiteDatabase db){
  71.             Log.e("i", "TABLE BEING CREATED -------------------------------- " + getTableName() );
  72.             db.execSQL(getDBSetup());
  73.         }
  74.     }
  75.    
  76.    
  77.     // return a string of table being created
  78.     protected abstract String getDBSetup();
  79.     // return a string containing the desired table name
  80.     protected abstract String getTableName();
  81.     // return a string array of the db collum names
  82.     public abstract String[] getColNames();
  83.     // return a string array of user visible collum names, empty strings for the hidden ones
  84.     public abstract String[] getVisColNames();
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement