Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLiteOpenHelper - different class for every table?

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

like image 774
developer82 Avatar asked May 19 '14 17:05

developer82


1 Answers

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:

  • One database helper class.
  • The helper involves separate table-specific helper classes which are not SQLiteOpenHelpers but just doing part of the work for the top-level database helper.
like image 185
laalto Avatar answered Sep 22 '22 04:09

laalto