I was reading this article (http://www.vogella.com/tutorials/AndroidSQLite/article.html) to learn about SQLite databases in android apps.
In the article he has a tip:
It is good practice to create a separate class per table. This class defines static onCreate() and onUpgrade() methods. These methods are called in the corresponding methods of SQLiteOpenHelper. This way your implementation of SQLiteOpenHelper stays readable, even if you have several tables.
if I understand this tip correctly I should have a class for each table that I have in my database?
Is that really the best practice?
If so, what about complex queries that uses multiple tables? how do I manage that if the creation is in different classes?
How do I correctly keep the database version? for each table change I will change the database version number?
Thanks
SQLiteOpenHelper
manages database files, not tables. You manage the tables yourself with the given database lifecycle callbacks (onCreate()
, onUpgrade()
).
Quickly reading one could interpret that the author advocates creating a separate database helper for each table (I did at first), but that's not the case. That would have been bad advice.
To reiterate the author's intent:
SQLiteOpenHelper
s but just doing part of the work for the top-level database helper.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With