1

I want to clear my SQLite db every time I hit a particular spot in my application.

I intended on just making a method that I could call called resetTables(), but this seems to be more challenging than I expected because I don't really know where to place it. Here is a snippet.

@Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);

        }
        public void reset(SQLiteDatabase db) {

            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        }

I'm getting a yellow line under reset and I can't call this method in my code. Any ideas?

Note this question is similar, but couldn't get it to help me.

This worked:

public void resetTables(){
        mDb.delete(TABLE_NAME, null, null);
    }
Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • "I can't call this method in my code" -- why can't you? – CommonsWare Oct 17 '12 at 12:51
  • The method reset() is undefined for the type DBAdapter – EGHDK Oct 17 '12 at 12:52
  • I hit a particular spot in my application, what is that spot? – jeet Oct 17 '12 at 12:52
  • 1
    Then apparently you did not define `reset()` on `DBAdapter`. What class did you define it on? – CommonsWare Oct 17 '12 at 12:55
  • "particular spot" is when I hit a button to start a "new game". I defined reset() right below onCreate. EDIT: So reset() was being defined in DatabaseHelper, so I moved it down and now I don't get any errors in my DBAdapter class. But when I try to call it in my code I need a parameter to pass through, but I don't have any to send in. `test_db.reset();` – EGHDK Oct 17 '12 at 12:57

2 Answers2

1

first create one method where you create your database and table.

 /**
     * Re crate database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_NAME, null, null);
        db.close();
    }

and call above method using on your button click event.

Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
0

i have done it by this way...

@Override
public void onOpen(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    super.onOpen(db);

     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
}

and yes it definitely worked for me..:)

Kalpesh Lakhani
  • 1,003
  • 14
  • 28
  • But in my application, I open my database many times, and don't want to reset it when it's opened. I want to create a separate method like `reset()` – EGHDK Oct 17 '12 at 13:05