samedi 4 juillet 2015

Android SQLite code feedback

I have been through a few sqlite tutorials and wrote this code on my own to reinforce the principles. The tutorials I went through varied widely in a few areas so this is what I came up with as a combination of everything.

Other than the two classes I have listed below, All I have is one activity that adds, remove, inserts, update and display data from the database.

I am seeking any feedback whatsoever. However I have a few specific questions. Thanks in advance.

  • When should I close the helper class or does garbage collection deal with it automatically?
  • Should I create a Boxer POJO(Plain old java object to pass boxer data to and from the DAO?
  • Was the DAO implementation Efficient?
  • Does the code deviate from Java and Android best practices in anyway?
  • Any constructive advice is welcome

Helper Class

public class BoxScoresHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "boxing_scores.db";
    private static final int VERSION = 1;
    private static BoxScoresHelper instance = null;

    public static BoxScoresHelper getInstance(Context context){

        if(instance == null){

            instance = new BoxScoresHelper(context);

        }

        return instance;
    }

    private BoxScoresHelper(Context context) {
        super(context, DB_NAME, null, VERSION);



    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(createBoxerSQLString());

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("Drop Table If Exists " + BoxerDAO.TABLE_NAME);
        onCreate(db);

    }

    private String createBoxerSQLString(){

        String boxerCreateString = "create table " + BoxerDAO.TABLE_NAME +
                "(" + BoxerDAO._ID + " Integer Primary Key AutoIncrement, " +
                BoxerDAO.BOXER_NAME +  " Text Not Null, " +
                BoxerDAO.WEIGHT_CLASS + " Text Not Null, " +
                BoxerDAO.WINS + " Integer Not Null, " +
                BoxerDAO.LOSSES + " Integer Not Null);";


        return boxerCreateString;
    }

}

DAO Class

public class BoxerDAO {

    public static final String TABLE_NAME = "Boxer";
    public static final String _ID  = "_id";
    public static final String BOXER_NAME = "boxer_name";
    public static final String WEIGHT_CLASS = "weight_class";
    public static final String WINS = "wins";
    public static final String LOSSES ="losses";

    private final BoxScoresHelper myScoresHelper;
    private SQLiteDatabase myBoxerDB;

    public BoxerDAO(Context context){

        myScoresHelper = BoxScoresHelper.getInstance(context);

    }

    public Cursor query(String[] projection,String selection,String[] selectionArgs, String orderBy){

        Cursor cursor;
        myBoxerDB = myScoresHelper.getReadableDatabase();

        cursor = myBoxerDB.query(TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy);

        //myBoxerDB.close();

        return cursor;


    }

    public Cursor queryAll(){

        Cursor cursor;
        myBoxerDB = myScoresHelper.getReadableDatabase();

        cursor = myBoxerDB.rawQuery("Select * From " + TABLE_NAME, null);

        //myBoxerDB.close();
        return cursor;

    }

    public int delete(int id){

        int rowsDel;
        myBoxerDB = myScoresHelper.getWritableDatabase();

        rowsDel = myBoxerDB.delete(TABLE_NAME, _ID + " = " + id , null);

        //myBoxerDB.close();
        return rowsDel;

    }

    public long insert(ContentValues values){

        long insertId = -1;

        myBoxerDB = myScoresHelper.getWritableDatabase();

        insertId = myBoxerDB.insert(TABLE_NAME, null, values);

        //myBoxerDB.close();

        return insertId;

    }

    public int update(ContentValues values,String selection, String[] selectionArgs){

        int updatedRows;
        myBoxerDB = myScoresHelper.getWritableDatabase();

        updatedRows = myBoxerDB.update(TABLE_NAME, values, _ID + " = " + selection, selectionArgs);
        //myBoxerDB.close();

        return updatedRows;

    }   

}

Aucun commentaire:

Enregistrer un commentaire