What's the easiest method to use the sqlite database in android?

The sqlite database file (copy it the very first time in to the application atmosphere)


Creating the tables in code (in database helper's onCreate())

My database has 6 tables which is empty the very first time. I request this because I wish to update my database later on and may wish to know a great way with this.


You need to create (in code) it the very first time it's used. Android provides the SQLiteOpenHelper class that needs to be used for this. SQLiteOpenHelper defines the next techniques:

onCreate(SQLiteDatabase db): invoked once the database is produced, this is when you may create tables and posts for them, create sights or triggers.

onUpgrade(SQLiteDatabse db, int oldVersion, int newVersion): Invoked when the used database is over the age of the present version. Handle here the upgrade stuff (data migration, table creation/deletion)

See for a great tutorial: http://www.codeproject.com/KB/android/AndroidSQLite.aspx

If you do not have a much your db up-to-date by user interaction, the file may be the smartest choice, mainly in the situation that you've a large amount of data to place (file copying versus a lot of card inserts).

However, should you have a much some data changed or added through the user, the file approach works only within the first release.

Whenever you will have to improve your schema or add new data (delivering upgrading), you will have to take into account that the present data may be transformed or overflowing by some items that the customers will anticipate finding Following the upgrade.

So changing the file isn't a choice any longer.

Just in case you should utilize the sqllite assistant approach, I'd like to hear some feedbacks on my small sqllite assistant code generator that you could find here: github

Not specific to SQLLite or android, however have done a Home windows buying and selling application where customers could save lower Xml 'documents' - ie: a custom view saving their confirming preferences as well as other flags that could then be shared round the team. On startup a user's profile was loaded as well as their documents parsed to personalize the UI.

The applying was to possess a release every 3 days and existing documents needed to utilize the brand new application. It was an issue as from time to time the XML schema transformed leading to new or erased fields.

The answer we emerged with ended up being to create an abstract type known as Patcher. Each release might have a number of DerivedPatcher types by using it that have been operate on the very first load after an update. The Patcher might have an abstract approach to patch the XMl documents. Meaning an XML document could be loaded along with that old schema and upgraded, saved in-place while using new schema. The Patcher would in addition have a rollback approach to allow unrolling if the error happened.

Exactly the same approach could be relevant to tables inside a database. Essentially if you're able to produce a patcher or PatchManager to serialize key tables to XML in memory, then apply the DB changes and write the information back, you are able to achieve database migration inside a generic, re-functional way.

A vital advantage of this process is it may be examined on developer Computers before deployment, as long as you can get some sample SQLLite data. Understanding how your customers make use of your application is essential here.