BookShelf app: adding book cover

In this tutorial we will add a field to our model and the database.




Step 1: Add coverUrl field

public class Book {
   private int id;
   private String title;
   private String author;
   private String isbn;
   private String coverUrl;



Step 2: Create getters and setters.

Step 3: increment database version since we are modifying the structure:

 private static final int DATABASE_VERSION = 2;


Step 4: in class BookSqlHelper add field

private static final String FIELD_COVER_URL = "cover_url";
   private static final String[] COLUMNS = { //
   FIELD_ID, // 0
         FIELD_TITLE, //  1
         FIELD_AUTHOR, // 2
         FIELD_ISBN, // 3
         FIELD_LOCATION, // 4
         FIELD_COVER_URL // 5   };


Step 5: Change all CRUD methods.

for example:


   @Override
   public int update(Book book) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put(FIELD_TITLE, book.getTitle());
      values.put(FIELD_AUTHOR, book.getAuthor());
      values.put(FIELD_ISBN, book.getIsbn());
      values.put(FIELD_LOCATION, book.getLocation());
      values.put(FIELD_COVER_URL, book.getCoverUrl());




As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles