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 7873149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:25:44+00:00 2026-06-03T02:25:44+00:00

I have been trying to get this right but i dont know what i

  • 0

I have been trying to get this right but i dont know what i am missing. I have an android app and I wish to add 1 more table to it.However i am not able to do it and I have no Exceptions as well(dont like these silent killers!!).

Below is my code my SQLiteHelper class

public class DbCreator extends SQLiteOpenHelper {

public DbCreator(Context context) {

    super(context, Constants.DB_NAME, null, Constants.NEW_VERSION);//NEW_VERSION=2

    this.myContext = context;
}

//Rest of code

//Checks if DB is present and create if reqd.
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database : " + e);

        }
    }

}

    //Check DB is present
    private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = Constants.DB_PATH + Constants.DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        Log.v("DB", "No DB");
        // database does't exist yet.

    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getResources().openRawResource(
            R.raw.diary_database);

    // Path to the just created empty db
    String outFileName = Constants.DB_PATH + Constants.DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

    //**This is the problem area
    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Constants.log(TAG, "Upgrading started..."+db.getVersion() );
    if (db.getVersion() == Constants.OLD_VERSION) {
            db.beginTransaction();
        Constants.log(TAG, "Upgrading started...transaction");
        db.execSQL("create TABLE Thought(  StartDate DATETIME, Content TEXT, EndDate DATETIME, Title TEXT )");
        db.setVersion(Constants.NEW_VERSION);//NEW_VERSION=2
            db.endTransaction();
        Constants.log(TAG, "Upgrading started...transaction finished");

    }

}

Worst part is i see all the logs happening and even the query runs successfully when i do it on console.


Edits

My DB does not gets updated :-

I have pulled my DB from emulator and even seen the logs from the version no.is not changed.I use below lines in my activity from to see DB version.

DbCreator dbCr = new DbCreator(this);
SQLiteDatabase myDataBase = dbCr.getMyDatabase();
Constants.log(TAG, "Db Version : "+myDataBase.getVersion());
  • 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-06-03T02:25:46+00:00Added an answer on June 3, 2026 at 2:25 am

    I strongly encourage you to switch to SQLiteAssetHelper, which has the whole package-the-database-in-the-app pattern worked out.

    Tactically, you are not committing your transaction. The proper recipe for multi-statement transactions is:

    try {
      db.beginTransaction();
    
      // do SQL here
    
      db.setTransactionSuccessful();
    }
    finally {
      db.endTransaction();
    }
    

    Without the setTransactionSuccessful() call, endTransaction() will do a ROLLBACK.

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

Sidebar

Related Questions

I have been trying to get this one section of my UI to immediatly
I have been trying to get this to work correctly and I think I
I have been trying to get this jsfiddle to work. So far without luck.
I have been trying to days now to get this website completed ... however,
This is crazy, I have been trying for hours to get this to work.
I've been trying to get this complex MYSQL query to work exactly right over
I have been trying to get this to work for about three solid days
I have been trying to figure this out for a while now, but every
I have been trying all day to get this to work and have been
I have been trying to get my Arduino/Eclipse environment setup. For some reason I

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.