user2636982
user2636982

Reputation:

Delete Sqlite Table on Activity start

How i can delete sqlite table when my application start.

my DataBaseHelper have:

@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
{
   _db.execSQL("DROP TABLE IF EXISTS CARS");
}

But when app start nothing is happening. I want to drop tables if they exist on every start of application.

Thanx

Upvotes: 0

Views: 1112

Answers (3)

nouseforname
nouseforname

Reputation: 748

OnUpgrade is only called if the DB_VERSION has changed, so the database need to be modified or something.

In your case i suggest to create a single Method which is doing the job and call that from the activity method onCreate. Then it it will be called on every start.

Edit:

The Db Helper Class:

public static final  String DB_NAME = "MyDbName";
public static final int DB_VERSION = 1;

private SQLiteDatabase db;

public DbHelper(Context context ) {
    super(context, DB_NAME, null, DB_VERSION);
}

public void deleteTable() {
    if (db == null || !db.isOpen())
        db = getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS CARS");
}

Activity onCreate:

dbManager.deleteTable();

Upvotes: 1

Joaquim Ley
Joaquim Ley

Reputation: 4127

You can create a method that drops the table, and just call it on the "onCreate()" of your Activity.

the onUpgrade() method is only called when the DATABASE_VERSION is changed, so there is no guarantee that it will always be called on every start.

Upvotes: 0

Jose Rodriguez
Jose Rodriguez

Reputation: 10162

I work always on Android with OrmLite, but onUpgrade method is called when version of database change, and when you execute a query to sqlite database. You need to call a method to delete a database.

Upvotes: 0

Related Questions