1

I'm a beginner at SQLite Database making and I had just ran into a problem. I want to be able to completely reCreate my table, so i copied onCreate and renamed it reCreate with the exact same parameteres. What I want to ask is does the SQLiteDatabase that is passed into onCreate has to be a certain SQLite Database, because it seems that reCreate is not working (in a later function, it says "no such table" after using reCreate).

code that calls reCreate:

SQLiteDatabase db = null;
new EventDBHelper (this, 1).reCreate(db);

reCreate code:

//String used to create new player data table
private final String createDB = "CREATE TABLE IF NOT EXISTS " + DB_NAME + " ( "
        + I_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + NAME + " TEXT, "
        + TEXT + " TEXT, "
        + PIC_S + " TEXT, "
        + PIC_E + " TEXT, "
        + POINTS + " TEXT, "
        + PROB + " TEXT "
        + " ) ; ";
//String to delete table
    private final String deleteDB = "DROP TABLE IF EXISTS " + DB_NAME;

public void reCreate(SQLiteDatabase db){
//remove
db.execSQL(deleteDB);

//create
db.execSQL(createDB);

//initialize the database
db.insert(DB_NAME, null, insertValuesFromEvent (find_bill));
db.insert(DB_NAME, null, insertValuesFromEvent (find_bill2));
db.insert(DB_NAME, null, insertValuesFromEvent (salary));
}

I didnt find documentation for this.

2
  • So what you saying is you don't have an onCreate() method ? Commented Apr 2, 2014 at 17:45
  • No, I do. Its just the same thing as reCreate Commented Apr 3, 2014 at 3:50

1 Answer 1

1

Well, you're passing null as an argument to your reCreate() so obviously invoking methods on that won't work.

The SQLiteDatabase reference that gets passed to onCreate(), onUpgrade() etc. is the same that will eventually be returned by e.g. getWritableDatabase(). So if you want to run custom SQL on your database, run it on the return value of getWritableDatabase().

If you want to recreate your database, just uninstall your application so the old database file is removed. When you reinstall and run it, the first call to e.g. getWritableDatabase() will invoke your onCreate() and you should create the tables there.

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

3 Comments

Well, the thing is that I sort of need a button to remove the table. I just need to recreate a table. And its still not working if I enter in 'new EventDBHelper (this, 1).getWritableDatabase()' instead of 'null', telling me that Table does not exist. Im I doing soething wrong? If i dont delete the table (not pressing the button at my start activity), it works perfectly well.
I saw another question (stackoverflow.com/questions/8627303/…) and it talks about ContentProviders. Does that have any connections to my problem? PS: does pressing clear data work too for removing databases?
Table doesn't exist if you didn't create it. An empty onCreate() won't create anything. For deleting data, there's delete() for that matter - no need to drop and recreate tables. Content providers are something you can put on top of your database and won't help solve the problem. Clear data works as well like the uninstall trick.

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.