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

Ruby

Jeff Lange
Jeff Lange
8,788 Points

Where is database information stored?

I was watching this video: "A Quick and Dirty Example" (https://teamtreehouse.com/library/rails-models/introduction-to-activerecord/a-quick-and-dirty-example)

Instructor Hampton Catlin takes you through a quick creation of a blog app using Ruby on Rails. As part of the blog app, we created Posts (objects?) using the rails console. These Posts had titles and bodies, and I assume the that "title" and "body" became columns for information about the posts in a database.

When using Railss (localhost:3000) it clearly accesses the information we made in the rails console, correctly displaying the title and bodies of the posts.

So here's where I'm confused: for the life of me I cannot find where the information about the posts is stored, and how Rails is getting that information. I looked in the project folder in the \db directory, and checked all the files in there, but some of them are ruby files that just tell Ruby how to create a post object. The others are sqlite3 files (development.sqlite3, and test.sqlite3), but they are just full of numbers. I tried migrating those sqlite3 files into MySQL to see if I could find the information that way, but they end up being empty databases that have no tables or information of any sort (other than a name).

I'd really like to be able to read information that's stored in the database for a project, or even have the ability to manually add stuff to a database using MySQL that Rails can add into an app accordingly. Anyone know how this works??

2 Answers

Robert Ho
PLUS
Robert Ho
Courses Plus Student 11,383 Points

The information is actually stored in those sqlite3 files (development.sqlite3, and test.sqlite3), but you can't read them without the proper software (a GUI for example). The Firefox browser has an add-on (plugin?) called SQLite Manager that acts as a GUI and allows you to view your tables in the browser. The link is located here:

https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

Once installed, just go to tools, SQLite Manager, and load your development db file and you should see all the Posts tables.

As for converting data from SQLite3 to MySQL, I found this stackover flow answer:

http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql

You can follow the link for the list of converters and then just follow the instructions.

Hope this helps!

Jeff Lange
Jeff Lange
8,788 Points

Awesome! Thank you. Love being able to actually open up the file and see what's going on. That extension for firefox was simple and did the trick!

I haven't looked into the converter link you shared, but I probably will end up doing so later today.

Thanks again. It's pretty annoying that I just finished an entire section on Treehouse devoted to learning MySQL, but then Rails actually uses a different database client... -_- I do get to actually apply the stuff I learned about MySQL, right??

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

As far as I now, these relational databases do not store plain text files with columns in them. These .sqlite3 files are sqlite's binary files that contain the data and can only be read by the sqlite3 driver that you attached to Rails (it is attached by default, see your gemfile and the database.yml file). Think of it as compiled exe file in Windows - when you open it, you don't see the C++ source code that the developer wrote, but the computer knows what to do when it encounters all those seemingly random numbers and characters :)

Jeff Lange
Jeff Lange
8,788 Points

Ugh, a black box. I like to be able to open it up and look inside.

Thanks for the explanation though. :)