0

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

  • Why dont you use the onCreate to delete and create new? Or is it just one table not whole db? Also possible you create a new method and call it on app start – nouseforname Aug 04 '14 at 17:10
  • Just 3 tables, i must save 1 table, but i was think maybe if i cannot to delete just table, that i make two .db, one delete, and create new, and one save? –  Aug 04 '14 at 17:13
  • What library do you use to work with Sqlite? – Jose Rodriguez Aug 04 '14 at 17:19
  • @Joseph, to be honest i don't know , i am still noob with android. I have packege android.database.sqlite in android.jar (17) if that help? or can you help me how to check? –  Aug 04 '14 at 17:24
  • @Nick please see my answer, and if it helps mark it as your answer, thanks – nouseforname Aug 04 '14 at 17:29

3 Answers3

1

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();
nouseforname
  • 720
  • 1
  • 5
  • 17
  • Just one question how to define db? (sorry but i still learning) . I have onUpgrade(SQLiteDatabase _db..) but if i define that way, i must define in on activity create to,yes? –  Aug 04 '14 at 17:31
  • @nick sry, my mistake i'll add it – nouseforname Aug 04 '14 at 17:33
  • 1
    This can help you: http://stackoverflow.com/questions/7444327/how-to-initialize-sqlite-database-once-from-a-helper-class-in-android – Jose Rodriguez Aug 04 '14 at 17:34
  • Now i getting error: 08-04 17:50:37.045: E/AndroidRuntime(22940): FATAL EXCEPTION: main 08-04 17:50:37.045: E/AndroidRuntime(22940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.police.car/com.police.car.LoginActivity}: java.lang.IllegalArgumentException: Version must be >= 1, was 0 :( –  Aug 04 '14 at 18:06
  • @Nick You have to add the Version 1 at least. I added some code as sample – nouseforname Aug 04 '14 at 18:18
  • in your answer.. "db.execSQL("DROP TABLE IF EXIST CARS");" it's not EXIST.. you are missing an S. it's EXISTS ! :) – JozeRi May 19 '15 at 06:51
0

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.

Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52
0

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.

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42