How to reset the SQLite in android? - android

I want that my users can resett the aplicatiom, then I need to reset the sqlite database that I have created. I'm using "context.deleteDatabase("*");" and calling the method tahr create the data base againg.
The problem is: when I delet the database I have to close the app and open it to see the chages. If I dont close the app, I continue with the same database.

to delete your app database try this:
this.deleteDatabase("databasename.db");
the above statement will delete the db file.

How about just dropping all the tables instead?

Related

How can we delete database file from the users' devices?

I'm taking part in developing Android app, and I have a task to delete one useless corner. The problem is this corner has got its own database, and after deleting
the file with this db was left on the users' devices. How can I delete it from there during the next release?
For deleting data inside of firebase you can simply use
remove() or you can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.
If you know the name of database table you may delete it by exeuting sql drop table if exists ${table_name}.

destroy the database- android life cycle

Hello I am trying to figure out a way to destroy my database every time the application starts either by returning from the background or reopen after it was closed. The issue is that when I am trying to run my application for the first time I get a null and my application crushes.
My application:
The user is prompt with a login screen and I create the db, move to another activity to perform a query. From this activity the user cannot go back to login, and will be only able to go to the phone home screen. When I will go to the home screen and re open the application I want to wipe out the db and start like it is my first time the user uses the application.
I know that this is not actually practical for an application, but this is just for learning.
I tried to do db.close() and context.deleteDatabase(db.getPath()); in onResume and onRestart, but this always crushes my application. I also tried to check that 'db.getPath()!=null' , but this did not help either.
thank you
Since you are aware that is not the most practical way to use databases I guess I won't bug you for that.
What you want to do is not to call the method to delete the database content during on Resume() or on Restart(), you actually want to call that via on Stop().
The method you want to use is
db.delete(String table, string whereClause, String [] whereArgs);
Passing the name of the table you want to delete, i.e "user_food_database", a whereclause which would be null in your case since you want to delete ALL rows, and null for whereArgs.
Here's the official doc:
"Convenience method for deleting rows in the database.
Parameters
table the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
Returns
the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause. "
You can also call:
deleteDatabase(File pathToDatabase)
in case you have more than one table in your database, but recreating the database itself everytime is expensive, and deleting only the content may result better.
I suggest that you simply wipe the database, i.e., delete all data (or even drop the tables). This is not a direct answer to your question, but has the same effect.
Just drop all tables in the open() method of your SQLiteDatabaseHelper and recreate the tables by calling onCreate(db). Works of course only when you are opening the db only once per app start.

How can I recreate a database with default values?

In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.
Does anyone have any idea on how to reuse a database?
Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:
You can do it with the following DANGEROUS commands:
PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'table';
PRAGMA writable_schema = 0;
you then want to recover the deleted space with
VACUUM
and a good test to make sure everything is ok
PRAGMA INTEGRITY_CHECK;
If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android
Well basically that's not an Android specific question.
Firstly when do you want to recreate the database with default values and how to trigget it.
In an UI event like button click etc?
Or when you start/stop or destroy your activity?
In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.

How to delete all rows(data) from sqlite database while starting the activity in android

I have created the Simple Application of the sqlite database. In which i am successful to implement the insert data. But i want to delete all the data while starting of the activity.
I have use the cursor to fetch the data and display. But dont know how to delete all the data.
I have use this.db.delete(DATABASE_TABLE, null, null); to delete the all rows. but it will not delete my data that are added before in database.
I have implement the Database Example from this link: Here is the Link
So, Please let me know, what i have to do, to delete all the data while starting the activity.
i think in the starting of the activity firstly u need to check whether the cursor.getcount() > 0 then fire delete query.
hope may work for u...

SQLiteException: no such table

I am using my own SQLite3 database as opposed to creating a new one each time my app runs, as I have a few tables with static data that I am trying to display. I created my database and placed it in my assets folder. I then created my database helper and when I start my app I can open my database without problem but when I try to open my first table using the following code
private Cursor getData()
{
try
{
myDbHelper = new DatabaseHelper(this);
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query("exhibitor", FROM, null, null, null,null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
catch(SQLiteException e)
{
String err = e.toString();
return null;
}
}
It throws an error saying android.database.sqlite.SQLiteException: no such table: exhibitor: , while compiling: SELECT _id, EXHIBITOR FROM exhibitor ORDER BY EXHIBITOR but when I check the database exhibitor is there.
What am I missing?
Settings -> Applications -> Manage Applications -> (Click on your application) -> Clear data
Whenever you create new table in an existing database the table doesnt create because the OnCreate function of database handler will not be called everytime but only if required(like database not initiated). If that is not the case your newly created table actually hasnt created. Clear the data to force the db to instantiate itself.
Have you moved the database from the assets folder to /data/data/YOUR_PACKAGE/databases/ on the emulator?
This is a good detailed post about moving the database to /data/data/YOUR_PACKAGE/databases/ if it does not exist.
Here is another short and simple solution to it.
Clear Data and uninstall application from your device and re-install application in device...
Settings -> Applications -> Manage Applications -> Your Application Name -> Clear data
Using SQLiteOpenHelper.onCreate() does not create a new database every time your app starts - rather it creates a database if it does not already exist.
It sounds like you might have an empty database created by or for your helper (if there was no database at all, I might expect a different exception), and your separate database that you created outside of Android. Looks like you and your app are not looking in the same place for your data.
Just clearing the data did not work for me.
What worked was:
Uninstall app
Restart device or emulator
Disable Instant run (Not just the main group but all individual instant run settings)
Build -> Clean Project
Build -> Rebuild Project
hapend to me once
change your DATABASE_VERSION
if your DATABASE_VERSION =1 it will see just three table
if your DATABASE_VERSION = 2 it will see just more table but i really didn't know how many
good luck
I just had a simple mistake.
Reason:
Solution:
Just happened to me. I don't exactly know why but changing the DB_VERSION attribute to a bigger number made it work. The thing is: each time i'm changing the fields of the DB (attributes of the SQLiteDB class), i need to change that number.
Here is my answer according to the description of #ReivieraKid.
No, Uninstall, No Restart, Just checking the app memory if it is your real database, if return false, then copy the database to the memory again. Then You will All set. But to apply this method, you have to know the minimum size of your database.
https://stackoverflow.com/a/73332470/7608371

Categories

Resources