1. Download our Official Android App: Forums for Android!

Apps How to clear database?

Discussion in 'Android Development' started by vovs, Feb 11, 2011.

  1. vovs

    vovs Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Feb 9, 2011

    Hi, All..
    I have ChatApp.
    All message save in local database.

    When user press button "Log out" i need clear data about chats and contacts from DB.
    I am clearing my shared_prefs(Log Out) and call clearMethod():
    Code (Text):
    1. public void onClear(SQLiteDatabase db) {
    2.         db.execSQL("DROP TABLE IF EXISTS " + Contact.TABLE_NAME);
    3.         db.execSQL("DROP TABLE IF EXISTS " + ChatSessions.TABLE_NAME);
    4.         db.execSQL("DROP TABLE IF EXISTS " + ChatSessions.FK_TABLE_NAME);
    5.         db.execSQL("DROP TABLE IF EXISTS " + Message.TABLE_NAME);
    6.         onCreate(db);
    7.     }
    When i try login - i see all my previous chats(DB si CLEAR).

    When i call Settings > Manage Applications> Menu button > Filter by Running. > Select Myapplication > Force stop
    and try login - all OK - chatlist is clear!

    How to correct clear database?
    Calling Force stop - is very hard way ((
     

    Advertisement

  2. SoftWyer

    SoftWyer Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    114
    Joined:
    Dec 16, 2010

    Can't you just "DELETE FROM TABLE xxxx" ?

    Another option is to delete the whole database: context.deleteDatabase(DATABASE_NAME);
     
  3. vovs

    vovs Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Feb 9, 2011

    2SoftWyer

    If I run these command tables will be cleared. But App still running and I can view all data from DB in my app(chatlists, all contacts etc.. )...
    Only when I do "Force stop" and try run - then my app will be use clear DB.

    In these case my app is down, but in Settings > Manage Applications> Menu button > Filter by Running. > Select Myapplication
    button "Force stop" is active.....

    Does it possible to clear db and reinitialize my app to view clear lists(chat, contacts etc)?
     
  4. SoftWyer

    SoftWyer Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    114
    Joined:
    Dec 16, 2010

    I'm not sure what the problem is. When you say you can still see it, do you mean from within the application using "SELECT"? Or externally from a database viewer?

    If you "DELETE FROM TABLE xxx" and commit, then it should be gone. If you can still do a "SELECT" and see the data, then either you didn't delete the right data or you didn't commit and it rolled back.

    In my application I call the deleteDatabase(name) command and it physically deletes the database, I just then create a new one.

    When I tried to use DROP, it didn't seem to do anything, at least it didn't free up any physical space. Try DELETE and not DROP
     
  5. markbernaldo8

    markbernaldo8 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Feb 13, 2011

    Feb 13, 2011
    1
    0
    5
    Hair Loss Expert
    Balanga
    hi, i just want to know if you're asking how to delete the whole database or a single form? because it's so easy to clear the whole database even without asking, you just need to learn it from yourself.

    more exciting games here : Games | Fun Games
     
  6. idavis

    idavis Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    Feb 11, 2011

    Feb 11, 2011
    27
    4
    16
    Why don't you turn your code the initially reads the database, and populates the program into a method. Then call that method after you have cleared out the databases. Sounds like you are clearing the databases, but not reloading the data into the program (even blank data).
     

Share This Page

Loading...