Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:29:37+00:00 2026-05-23T14:29:37+00:00

I want to upgrade the database to a newer version. How would i go

  • 0

I want to upgrade the database to a newer version. How would i go about doing that in the Upgrade method it gives you?

 private static final String DATABASE_NAME = " nba";
private static final String DATABASE_TABLE = "bookList";
private static final int DATABASE_VERSION = 1;
private static String TAG = "Upgrading Database!";
public static final String KEY_BOOK = "book";
public static final String KEY_AUTHOR = "author";
public static final String KEY_ISBN = "isbn";
public static final String KEY_ROWID = "_id";
public static final String KEY_RATING = "rating";
public static final String KEY_STATUS = "status";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;


private static final String DATABASE_CREATE = 
        " create table " +  DATABASE_TABLE  + " ("
        + KEY_ROWID + " integer primary key autoincrement,  "
        + KEY_AUTHOR + " text not null, "
        + KEY_BOOK + " text not null, "
        + KEY_RATING + " text not null, "
        + KEY_STATUS + " text not null, "
        + KEY_ISBN + " text not null); ";

private final Context mCtx;


public DbAdapter (Context ctx){
    this.mCtx = ctx;



        }

        private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);


        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS user");
                db.execSQL("DROP TABLE IF EXISTS site");
                db.execSQL("DROP TABLE IF EXISTS site_user");
                db.execSQL("DROP TABLE IF EXISTS article");
                onCreate(db); 
    }
}
        public DbAdapter open() throws SQLException{

            mDbHelper =  new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }
        public void close(){
            mDbHelper.close();
        }

        public long createBook(String book, String author, String isbn, float rating, String status){
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_BOOK, book);
            initialValues.put(KEY_AUTHOR, author);
            initialValues.put(KEY_ISBN, isbn);
            initialValues.put(KEY_RATING, rating);
            initialValues.put(KEY_STATUS, status);

            return mDb.insert(DATABASE_TABLE, null, initialValues);

        }
        public boolean deleteBook(long rowId){
            return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

        }
        public Cursor fetchAllBooks(){          
            return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_ISBN,KEY_AUTHOR,KEY_ROWID, KEY_RATING, KEY_STATUS}, null, null, null, null, null);
        }
        public Cursor fetchBook(long rowId) throws SQLException{
            Cursor mCursor = 
            mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR,KEY_ROWID, KEY_ISBN, KEY_RATING, KEY_STATUS}, KEY_ROWID + "=" +
                        rowId, null, null, null, null);

            if(mCursor != null){
                mCursor.moveToFirst();
            }
            return mCursor;

        }
        public boolean updateBook(long rowId, String book, String author, String isbn, float rating, String status){
            ContentValues args = new ContentValues();
            args.put(KEY_BOOK, book);
            args.put(KEY_AUTHOR, author);
            args.put(KEY_ISBN, isbn);
            args.put(KEY_RATING, rating);
            args.put(KEY_STATUS, status);

            return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0;


        }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:29:38+00:00Added an answer on May 23, 2026 at 2:29 pm

    There’s no “magic bullet” for upgrading a database. The strategy your current implementation is fine except it will blow away all of the users data. The most elegant, yet complex solution, would be to assign a version number to the database by creating a special table to hold that value. Then, based on the version, the onUpgrade method executes a series of alter statements to modify the structure to meet the new version. I would do it incrementally and iteratively so that each version only needs to know how to upgrade from the previous version.

    That is to say, version 3 has no idea how to upgrade from version 1 to 2, but it does know how to upgrade from version 2 to 3. Or, if the new version is version 5 and you’re at version 1, you run the script to upgrade from 1 to 2, then 2 to 3, then 3 to 4 and finally from 4 to 5. Does that make sense?

    If you’re asking about how to modify table structure in SQL, check out the ALTER TABLE statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best option if I want to upgrade old C-code to newer
I want to bulk insert about 700 records into the Android database on my
A database needs to be upgraded and as part of the upgrade, I want
I have a database (MS SQL) with a column that contains a query string.
During an upgrade, I want to add a new table to my database, but
I want to upgrade the Windows 2003 x86 server which is running ASP.NET 2.0,
i have a windows client and i want to upgrade subversion from 1.5.3 to
I'm running Drupal 5 on my website and want to upgrade to V6. I've
So, I want to upgrade my development team to VS2008. As an enterprise, we
I want to update/upgrade the standard Leopard install of Sqlite3 to >3.5 to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.