I need some advice. I want to implement a set up, which has 4 static saved objects saved in a database. How would a user select one of these objects to use in a service... When using a SQLDatabase, do you have to create a database every time the app starts.
Any advice welcome.
Thanks
When the application starts for the first time the database is created.
The SQLite database saves the information you need into the database, and isnt erased until you allow the user to delete it or update the database.
You can find a very good tutorial on using a database here
EDIT:
It has means for you to allow the user to update the database also, such as information,names,numbers,ect.
Related
I'm new to android. I want to ask about SQLite database lifecycle. Basicaly, I want to include the insert data method and viewing data method in the main class (which basicaly called 1st when we open the app). Then I'm going to make some update to the database in another activity. What I want to ask is, when I'm going to open the app for the next time, which data that will be showed? Is it the 1st data that I inserted with insert method OR the updated one?
I don't understand exactly your question. So I hope this will answer it. There are 4 operations, you should implement: create, read, update, delete. For example when you have a contact table with id, name, phone number then you can create (John, 012345). If you then call the read method with id 1, you will get (John, 012345). But if you update this before you read it, you obviously get the updated item - the database didn't delete anything when you close the activity. I highly recommend you to create a new class to handle those crud-operations, because you will get crazy, when you wanna change the update operation and must search it in every single line from your app. I also recommend you to read this blog. It saved me a lot of time:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
As far as I understand your question,
my answer is -> You should see the updated data, because Db is same throughout the app.
The data that will be shown is the updated one , because SQL operations are immediate. It is very unlikely that onUpdate() will not update the database until and unless it encounters any error. you can see that itself using a CursorLoader attached with your cursor after running a query command. This can be done in an activity just displaying the contents of the database.
For database operations, It is beneficial to use Content Providers cause then you can extend your apps to widgets, share data with other apps etc.
I am trying to build an app that will use an SQLite database. My question is once the database is created, will all data still be there once the app is closed, i.e.: the database won't be overwritten when the app is restarted?
You, as a developer are the only one who have access to this database, thus it is only your code which will be allowed to change the state or content of this database.
Yes, the data will remain there.
The data will remain there because of your default create method. There is a status code. You can use SQLScout http://www.idescout.com/ to visualize the table and watch its change step by step.
Yes, data will remain in your application when you close the application. When you run the application the database create in the application.
But yes, if the user uninstalls, the application then user will lose the application. On the other hand, if you want to save the data when the application is uninstalled, then it's better to create the database in SDCard.
I have an application where a certain data (about 100 rows) has to be stored in db before every start of application. I do not want to insert it every time when user starts app. What is the solution? How to store datas? What is the best way?
The onCreate method is the one that is run once by every app on its launch. Here you can have your code to insert the values in the DB.
Also you can connect to your DB and see if it already exists and if not perform the creation and insertion.
Refer these link's for more idea on DB in Android...
http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android
My question is simple :
I have a "flag" stored in SharredPreference but when the user is doing an application update, this data is ereased. Is there a way to keep data stored even if the user is doing an application update ?
Well, as far as I know you have only two choices - write it in a file or save it in the database. I always put the data in the db, from witch they are loaded at program start. Just have a table with the prefs in it and you are sure, that your data is not deleted, even after updating. The database interface is easy to use and lean - therefor I use it.
I have an Android application which uses sqlite database to store textual data for a particular user. The data is read/written from/to the database from a service that runs periodically after every n seconds. My requirement is to clear data depending on age. Like, if the file is more than 24 hours old it should be deleted.
Checking the age of the file seems easy, just compare current time with the File creation time. The problem is:
where should I put this check and delete the file; inside application onCreate() or when the user logs in/ logs out? What would be an ideal place to trigger this logic from? Should I schedule an Alarm when the user logs in?
Should I delete the file or simply delete the rows? If I don't trigger the Cache clearance logic from login/logout, won't deleting the file cause problems, especially if the service is still trying to read and write from the database?
Please advice.
Thanks.
Well, this all depends on your logic for the application for the second part. I cant see why you would delete a database unless its just used to store temp data that does not matter. Either way the ideal place to do this check and delete is in the Data Access class thats opening the connection to the database. Below would be my logic...
Call to open DB
Check if DB file is old
If yes, delete it
Open Database (should create one if it does not exist)