1

I have the following Java Class

package com.sjhdevelopment.shaunharrison.myejuiceapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "EJuiceData.db";
public static final int DATABASE_VERSION = 1;


public MyDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

public void onCreate(SQLiteDatabase database) {
    database.execSQL("create table if not exists Inventory");
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        database.execSQL("ALTER TABLE Recipe ADD COLUMN NOTES TEXT");
    onCreate(database);
}

}

Then in my main activity I have the following;

public class Calculation extends AppCompatActivity {
   private MyDBHelper dbHelper;
   protected void onCreate(Bundle savedInstanceState)
   {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_calculation);
      try {
        dbHelper = new MyDBHelper(this);
        dbHelper.getWritableDatabase();

      }
      catch(Exception e)
      {
        showError("Error", e.getMessage());
      }
   }

However, when I debug the code to see if the database + tables are being created with a break point on the try and on the database.execSQL("create.... There debugger doesn't actually go into the MyDBHelper class to do anything, therefore I'm assuming that the database + tables aren't being created

I've looked online and it says that when a getWritableDatabase/readable is called the onCreate method should get called

Any idea's as to what I'm doing wrong?

1
  • leoderprofi suggestion is correct!! either [upgrade database version] or [uninstall and then re-install the app again]... Commented Oct 12, 2015 at 12:11

2 Answers 2

2

onCreate(...) is only called when the database is created for the first time. If you want to change your database you have to increase DATABASE_VERSION then it will jump into onUpgrade(...)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, if you want to look at your database look at this answer stackoverflow.com/a/21062291/4265739
0

It looks like your SQL is not correct onCreate. Instead of database.execSQL("create table if not exists Inventory"); it should be

database.execSQL("CREATE TABLE table_name 
                  (id_column INTEGER PRIMARY KEY
                  , column_name TEXT);";

You need to manually created every table with all the corrected columns. They will be created once you call the database for the first time.

5 Comments

There would be a distinctive error message if the sql in oncreate is not correct.
ok so lets assume for now the tables have been created, if I was to load data from the tables I normally used SQLiteDatabase db; , db = openOrCreateDatabase("EJuiceData", Context.MODE_PRIVATE, null); , Cursor c = db.rawQuery("SELECT NAME FROM Recipe", null); However now that i'm using SQLiteHelper I imagine it's changed?
nope you still do the same as you described above. Also don't forget the end the query with ; when you do database.execSQL
Sorry @Aegis I have done all that, I just cut it out so that there wouldn't be so much code in the question I've made sure all queries end with ; However when I'm running the code now it's saying that the tables do not exist? is there a way to delete the database so that i can run it from fresh
You can either remove the app which will remove all the app files, clear data which removes all app files but leaves the app installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.