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

  • Home
  • SEARCH
  • 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 7007081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:33:40+00:00 2026-05-27T21:33:40+00:00

Well, this is a question about best practise with Android SQL Lite DB’s. I

  • 0

Well, this is a question about “best practise” with Android SQL Lite DB’s.

I got my DBHandler which creates a SQLiteDatabase with a inner class called DBHelper like this:

    private final DBHelper dbhelper;
    SQLiteDatabase db = dbhelper.getReadableDatabase();

    // Call the helper  
    private static class DBHelper extends SQLiteOpenHelper {

    DBHelper(Context context){      
        super(context, DB_NAME, null, DB_VERSION);  
    }
        @Override   
        public void onCreate(SQLiteDatabase db){
            // create Table01       
            String sql =    "create table " + "... SQL stuff here";     
            db.execSQL(sql);
        }

        @Override   
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
            db.execSQL("drop table if exists " + Table01);
            onCreate(db);
        } 
    }

Also the DBHandler offers functions for adding / retrieving data into / from the tables like e.g. this:

public void insertOrIgnore(String table, ContentValues values){
    SQLiteDatabase db = this.dbhelper.getWritableDatabase();
    try{
        db.insertOrThrow(table, null, values);
    } 
    catch (SQLException e){
        e.printStackTrace();
        Log.e(TAG, e.toString());
    }
    finally {
        db.close();
    }
}

public Cursor getTable1(){
    SQLiteDatabase db = dbhelper.getReadableDatabase();
    Cursor cursor = db.query(Table1, null, null, null, null, null, TABEL_ID + " ASC");

    return cursor;
}

When i need data from the DB i create a DBHandler Object and use its methods.

so, on first application run i call the insertOrIgnore method and add the data i want to use later on.
Whenever i make changes to the DB, i need to uninstall the APP and reinstall it for the changes to take effect. that sucks, so i want to use the DB_VERSION to handle that stuff. After changes to the database (schema OR data) have been made i change the DB_VERSION ID to a higher number.

So my questions are:

  1. If newDBversion > oldDBversion, SQLiteDatabase dosn’t use onCreate but onUpdate. That works fine, but ONLY the schema is updated
    and it results in blank tables. I want to know the best practise so
    after a onUpdate the data methods from DBHandler are called again.
    Please keep in mind that DBHelper is a inner class of
    DBHandler.
  2. For my understanding the SQLiteDatabase onCreate method is ONLY called if the DB is not already present and the onUpdate method is
    only called if newDBversion > oldDBversion, is that correct?
  3. Does DBHandler have to be static or a singleton? or is it just fine without, since the SQLiteDatabase class handles multiple read/writes by itself?

I’m open minded for better solutions, but from what i found on the net and in some android books, that kind of adding data to a SQL DB is the way its done. But nobody explains how to update the data (NOT the schema, which kinda is done automaticly whenever dbversion changes)

  • 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-27T21:33:41+00:00Added an answer on May 27, 2026 at 9:33 pm

    I can answer the third question because I research a little on this question. I decided that it is good to have the following way of defining these classes (look at my code)

    public class DbWordsAdapter {
    
    private static final String TAG = DbWordsHelper.class.getSimpleName();
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    private static DbWordsHelper dbWordsHelper; 
    private Context context;
    private SQLiteDatabase mDb;
    
    // database constants
    private static final String DB_NAME = "words.db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_WORDS = "words";
    
    // database columns names
    public static final String C_ID = BaseColumns._ID;
    public static final String C_WORD = "word";
    public static final String C_EXPLANATION = "explanation";
    public static final String C_CREATION_DATE = "creation_date";
    
    //Sql Statements
    static final String CREATE_TABLE_WORDS = "create table " + TABLE_WORDS
            + "(" + C_ID + " integer primary key autoincrement, " + C_WORD
            + " text not null, " + C_EXPLANATION + " text, "
            + C_CREATION_DATE + " date not null)";
    static final String DROP_TABLE_WORDS = "drop table if exists"
            + TABLE_WORDS;      
    static final String[] ALL_COLUMNS = { C_ID, C_WORD, C_EXPLANATION,
            C_CREATION_DATE };
    static final String ORDER_BY_DATE = C_CREATION_DATE + " desc";
    static final String ORDER_BY_ALPH = C_WORD + " asc";
    static final String ORDER_BY_RANDOM = "random() limit 1";
    
    
    /*
     * Inner class that manages database creation and management
     */
    private static class DbWordsHelper extends SQLiteOpenHelper {
    
        private DbWordsHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d(TAG, "SqlCreate Statement: "
                    + CREATE_TABLE_WORDS);
    
            try {
                db.execSQL(CREATE_TABLE_WORDS);
            } catch (SQLException e) {
                Log.e(TAG, "Error while creating database" + TABLE_WORDS, e);
            }
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
            // Here in the future should be method that change the schema of the
            // database. Now we just delete
            try {
                db.execSQL(DROP_TABLE_WORDS);
            } catch (SQLException e) {
                Log.e(TAG, "Error while updating database" + TABLE_WORDS, e);
            }
    
            onCreate(db);
    
        }
    }
    
    public DbWordsAdapter(Context context) {
        this.context = context;
    }
    
    public DbWordsAdapter open () {
        if (dbWordsHelper == null) {
            dbWordsHelper = new DbWordsHelper(context);
        }
        mDb = dbWordsHelper.getWritableDatabase();
        return this;
    }
    
    public void close () {
        dbWordsHelper.close();
        dbWordsHelper = null;
    }
    
    //
    //Following methods implement CRUD operations for this database
    //
    
    /*
     * This method creates new Word in the database.
     * 
     * @param word that we create
     * 
     * @param explanation of the word
     * 
     * @return id of the word
     */
    public long createWord(String word, String explanation) {
        Log.d(TAG, "createWord method");
        ContentValues newWord = createValue(word, explanation,
                createDateString(new Date()));
        return mDb.insert(TABLE_WORDS, null, newWord);
    }
    

    In your application in an appropriate place you simply call dbWordsAdapter.open(); then your db transaction (for instance, dbWordsAdapter.createWord(word, explanation)) and close database. Thus, for activity you can open DB in onResume() method and close in onPause()

    Considering the second question I think you are rigth.

    For the first question I do not know. Maybe it will be good to see how this implemented in other applications.

    Update: Answer for the first question
    In the documentation and other resources I’ve found the information about the update of the database. You can use sql statement alter table to insert new columns into your database.

    If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table.

    So, basically algorithm for updating in onUpdate should be the following:

    1. At first, according to the oldVersion and newVersion you define what
      should be updated.
    2. Then if you simply wants to add columns then you call ALTER TABLE
      ADD COLUMN sql statement. There are some limitations on that. You
      can find them here.
    3. For deletion and renaming you can use the following advice: If you
      rename or remove columns you can use ALTER TABLE to rename the old
      table, then create the new table and then populate the new table
      with the contents of the old table.

    Hope this helps!

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

Sidebar

Related Questions

Well, first of all sorry about this question it must be pretty straight forward
Sorry this is not a very well defined question, I am thinking about an
(This question is about the best way to temporarily and programatically keep new records
This is a question about how best to do DI, so it's not tied
This is another question about well disposing objects from .NET. After having read a
I was reading about this person's interview at a well-known search company. http://asserttrue.blogspot.com/2009/05/one-of-toughest-job-interview-questions.html He
This question would probably apply equally as well to other languages with C-like multi-line
I believe this question applies equally well to C# as to Java, because both
I know this question may well be the silliest question you've heard today, but
Please bear with me if this question isn't well formulated. Not knowing is part

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.