developer82
developer82

Reputation: 13713

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

Upvotes: 6

Views: 3231

Answers (1)

laalto
laalto

Reputation: 152817

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.

Upvotes: 9

Related Questions