How to access default Rails sqlite db?
Ruby on-RailsRuby on-Rails-3SqliteAptanaRuby on-Rails Problem Overview
I would like to view the data in my DB while developing with Rails (actually in all 3 of them development, test and production). I have not touched the configs, so it should be easy, but I was not able to find any usable info.
I have no idea what the connection string could be or where to enter it, since Aptana (v.3) seems to lack the good old data source explorer view I know from Eclipse. Could someone point me into the right direction?
EDIT: I am working on linux - Mint 12
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
You have neglected to mention the OS you are using.
One way is to use the sqlite3
command in your terminal.
sqlite3 db/development.sqlite3
However, for things like inspecting your rows, you would be better using a rails console.
rails c
> User.all # Where user is your model.
NOTE: Do not change your DB schema directly through sqlite3, something you may be used to if you come from a different web stack background. This is because the next time you run the migrations, the state will be different to what rails expects.
Solution 2 - Ruby on-Rails
Rails 3 provides a generic command for accessing the correct database client and pass in the name of the correct database for your current environment. This command is rails dbconsole
which can be shortened to rails db
$ rails db
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
This command does not offer much more than Gazler's answer and in fact his advice to use the console is good advice however the plus side for this method is that it will use the correct client if your DB is different in other environments.
Solution 3 - Ruby on-Rails
use
SQLite> .tables
this will give you the list of all tables exist in the selected database
@@to activate the consol
SQLite> rails dbconsole
@@to show tables
SQLite>.tables
@@ to show all rows in a table
SQLite> select * from posts
Solution 4 - Ruby on-Rails
There is a great application to browse sqlite3 databases. SQLite Database Browser.
P.S. You mentioned that you are using Aptana studio. I have started my RoR learning with this IDE as well but later have discovered Sublime Text and never wanted to use anything else since, I advise you to check it out.
Cheers
Solution 5 - Ruby on-Rails
To view data on DB, I used an SQLite client called DB Browser for SQLite,here is the link
There is a Linux version of this app too. There should be a database file with sqlite
extension on db
directory of the app. On DB Browser, select Open Database option and choose that file and you should be able to view the data.
Solution 6 - Ruby on-Rails
You can have online access to your database if you use activeadmin
.
Just add the gem activeadmin-sqlpage:
gem 'activeadmin-sqlpage'
And create activeadmin page:
# file app/admin/sql.rb
ActiveAdmin::SqlPage::register
Restart your server. Then go to admin panel and navigate the menu SQL
. Enter any sql command and press Ctrl+Enter
or Submit
button.
Solution 7 - Ruby on-Rails
Open terminal and type this command. This will open a rails console to query the database.
rails c
To get list of all the models you can use the following command
ActiveRecord::Base.connection.tables
> example: ["schema_migrations", "ar_internal_metadata", "categories", > "items"]
From the list of models, you can get first, last or all records.
Category.all
Solution 8 - Ruby on-Rails
If you are using RubyMine IDE, you can access the sqllite datasource from there. You can run queries or edit data in the database using the GUI.