Android SQLite CRUD Operation || Android Java

 First we Will Create Database Helper Class

DatabaseHelper.java

public class DatabaseHandler extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "countryData";


    private static final String TABLE_COUNTRY = "Country";

    private static final String ID = "id";

    private static final String COUNTRY_NAME = "countryName";


    public DatabaseHandler(Context context){

        super(context,DATABASE_NAME,null,DATABASE_VERSION);

    }


    @Override

    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String CREATE_COUNTRY_TABLE = "CREATE TABLE " + TABLE_COUNTRY + "("

                + ID + " INTEGER PRIMARY KEY,"

                + COUNTRY_NAME + " TEXT )";

        sqLiteDatabase.execSQL(CREATE_COUNTRY_TABLE);

    }


    @Override

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_COUNTRY);

        onCreate(sqLiteDatabase);

    }


    public void addCountry(CountryModel countryModel){

        SQLiteDatabase db = this.getWritableDatabase();


        ContentValues contentValues = new ContentValues();

        contentValues.put(COUNTRY_NAME,countryModel.getCountryName());


        db.insert(TABLE_COUNTRY,null,contentValues);

        db.close();

    }


    public List<CountryModel> getAllCountries(){

        List<CountryModel> countryList = new ArrayList<>();


        String selectQuery = "SELECT * FROM " + TABLE_COUNTRY;

        SQLiteDatabase db = this.getWritableDatabase();


        Cursor cursor = db.rawQuery(selectQuery,null);


        if(cursor.moveToFirst()){

            do {

                CountryModel countryModel = new CountryModel();

                countryModel.setId(Integer.parseInt(cursor.getString(0)));

                countryModel.setCountryName(cursor.getString(1));

                countryList.add(countryModel);

            } while (cursor.moveToNext());

        }

        return countryList;

    }


    public CountryModel getCountry(int id){

        SQLiteDatabase db = this.getWritableDatabase();

        String selectQuery = "SELECT * FROM " + TABLE_COUNTRY + " WHERE " + ID + "=" + id;


        Cursor cursor = db.rawQuery(selectQuery,null);


        if(cursor != null)

            cursor.moveToFirst();


        CountryModel countryModel = new CountryModel(Integer.parseInt(cursor.getString(0))

                , cursor.getString(1));


        return countryModel;

    }


    public int updateCountry(CountryModel countryModel){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(COUNTRY_NAME,countryModel.getCountryName());


        return db.update(TABLE_COUNTRY,contentValues, ID

                + " = ?",new String[]{ String.valueOf(countryModel.getId())});

    }


    public void deleteCountry(CountryModel countryModel){

        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_COUNTRY,ID + " = ?",new String[]{ String.valueOf(countryModel.getId())});

        db.close();

    }

}


Note : Above Class Contains All the CRUD Methods.

Watch Details Tutorials on Our Channel -- DREAM DEVELOPERS

Post a Comment

0 Comments