Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence CRUD Operations with SQLite Creating New Data

Hey I am starting to lose touch with what is being taught here. Is the memedatasource class necessary to make?

Is it necessary to make the memedatasource class? What is it doing? What is every class doing actually?

I want to store data in a sqlite database. I have made a DatabaseHelper class. I have a main activity which interacts directly with the DatabaseHelper class. What should each of my classes do? I am not able to relate.

Oziel Perez
Oziel Perez
61,321 Points

This is why I don't like new concepts being taught with a full blown project already made. I always prefer to start from scratch, and THEN later get into a stage where you work with an actual project that is pre-made.

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

It's a late response, but sharing this in case someone else is struggling with grasping the concepts.

It's not necessary to create the MemeDataSource class. But it's a best practice for separating different responsibilities of the code as Evan mentioned in this or one of the previous videos. You can look at the two classes MemeSQLiteHelper and MemeDataSource like f.e. database level 1 access and database level 2 access respectively.

That's a very simple explanation, but it might help in grasping the concepts. The level 1 class is solely for the purpose of defining the tables, column names, types etc, while the level 2 class is where all the CRUD (Create Read Update Delete) SQL code will go (the code that actually manipulates the records in the database). And the good thing is you'll never interact with the database outside of these two classes so the code stays clean.

For the purposes of this course, you shouldn't worry too much about other UI classes (activities, fragments). Imagine if working on a team and you are the person responsible for the database interaction. So a team member who's working on the UI is asking you to create a method for adding Memes in the database. He doesn't care how you implement it as long as it works, and you don't care where he'll call the method from (that's how I'm seeing this course so far, trying not the bother with the "unimportant things" and focus on the database related code only). You just come up with the create(Meme meme) method Evan just added in the level2 class (notice that I'm not even trying to remember the class name here). It needs to accept a Meme object as a parameter, and make sure that that object is inserted as a record in the appropriate table in the database.

And that same team member will ask you for a method that returns a list of Meme objects from the database and you'll just add that method in the level 2 class, share the method name with the team member and you are both moving on. Again, he/she doesn't care how the method works as long as it returns the required list, and you don't care where that method is being called and how that team member is using the list of Meme objects to display it on the screen.

I think I explained why the separation of code responsibilities is an important thing, even if you are the sole developer of the app. You just add the method your app needs in the level 2 class and forget about it, it just works and it's not mixed with other code responsible for the UI or something non related to database access. Your level 2 class is responsible for database operations and that's it.

The important thing for the so called level 1 and level 2 classes is: The level 1 class should extend SQLiteOpenHelper, have a constructor accepting a context, call super with that context adding the database name and version. The level 2 class should have a constructor accepting context which should also be kept as a field of the level 2 class, then have a field of the type of level 1 class which will be initiated in the constructor in a usual way with the new keyword and with the same context parameter.

That should be the basic setup for working with databases in android as far as I can tell from this course so far. I hope I made things clearer for everyone who is struggling with this. I know it made it a bit clearer for me at least :).