I'm toying with the Android SDK, with a view to writting a simple app for friends (and maybe for sale).
This app will search a database for keywords and dispaly the results on the screen, I've had a look at the searchabul dictonary and the notepad demo applications, but I'm still a bit unsure some things.
I know I need to write a class that extends the SQLiteOpenHelper, and use that to create the database, however how do I check if the database already exists?
Does onCreate get called on installation or every time an instance of the class is created? is the easyist way just to try and add the database each time and catch any errors (feels a little dangerous to be makeing the assumption every error will be due to the database already existing).
Thanks in Advance.
Your SQLiteOpenHelper will handle the creation of the database. Once the db has been created on the users phone SQliteHelper will be happy until you change the database version number.
As long as you put your db creation code in the onCreate method of a SQLiteOpenHelper you will be fine.
Have a look at the onUpgrade method from the Notepad demo as you need to write some code that handles what happens when your db tries to upgrade itself (it will do this when you change the db version number)
Whenever you call helper.getWritableDatabase() , onCreate method is called.
Related
Am a newbie to android development. I would like to have suggestions for setting up database during installation and populate the data in the tables.
As tables creation, data population in tables is a one time process which will be done during App installation, how can we write a program which will meet this requirement.
Thanks in advance!
You cannot setup database during installation. You can however do one of two things:
Ship your application with a ready made database.
Have your database created for you the first time it is needed.
If you prefer to go with option 1, you should extend an application class and have your databases created for you on the onCreate() call there. Refer to this for help. If you want to use option 2, you should understand the database lifecycle, most important thing being, when you are trying to access your database but it is not there, the system calls the onCreate() method of the class that extends SQLiteOpenHelper class. Refer to this url.
I'm creating an app which uses a database.
I'm refining the database, fixing errors and so on. But the onCreate() method of my helper is called only once. So, after the first test, the app's still using the old, and wrong database.
I can implement the onUpgrade() method, but this seems odd to me, since I'm actually fixing errors and I'll find many of them. Is this the right way to do it? Playing with database version numbers?
Is there any simpler method?
When your app starts, you can physically delete the database:
File myDB = getDatabasePath("my_database.db");
myDB.delete();
The next call you make to your database ContentProvider will then create the database from scratch.
If your are testing/creating your app, then just delete your database in onCreate() method on your Activity:
this.deleteDatabase("your_database_name");
But for the future release of your published app, i suggest to learn and implement the method onUpgrade().
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'm developing a sqlite database in my android application. I need it can grow easily, so I need that in future upgrades, I could change the database, etc.
I want to know if when I change the data base version in the sqlite creation method, it creates a new file of the database. If it does, then, in the onUpgrade I should migrate all the data, isn't it?
In conclusion, what onCreate does exactly? Does it create a new file of the database? Or does it modify the actually one?
I'm asking this because I dont want that the onCreate creates a new file... I want to alter the actual data base only.
Thanks
OnCreate is only called when a database needs to be created for the first time.
OnUpgrade is called if the database already exists and the version numbers do not match. You should use OnUpgrade to alter your database from the old version to the new version.
I am assuming you are referring to OnCreate method in SQLiteOpenHelper. It gets called when you request for a writable or readable database and the the database needs to be created. We do not call the oncreate method directly. So if a database is already existing and we request for a writeable or readable database --- no new database gets created.
Refer to these links for more information
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
http://developer.android.com/resources/tutorials/notepad/index.html
Hi I am new to android app development and have no idea of SQLite database.
Are The tables we create and the records we enter to the SQLite database tables happening run time. That means once create tables and insert data, do we have to do the same process again when exit and open the app again?(Only the database, tables and records remain until we open the particular app).
Thank you
Anything you do to the database is persistent. It will contain exactly the same data after your app shuts down and is later restarted.
The database and tables will remain after the app exits.
You can create a class that extends SQLiteOpenHelper to help.
Override the onCreate() and onUpgrade() methods there. onCreate() will get called only when your db doesn't exist yet. You can then execute sql to create the tables etc. onUpgrade will get called when you pass a new version number to the super() constructor.