Ruby-on-rails 如何访问默认的 Rails sqlite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10171673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:13:32  来源:igfitidea点击:

How to access default Rails sqlite db?

ruby-on-railsruby-on-rails-3sqliteaptana

提问by kostja

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.

我想在使用 Rails 进行开发时查看我的数据库中的数据(实际上在所有 3 个开发、测试和生产中)。我没有接触过配置,所以应该很容易,但我找不到任何可用的信息。

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?

我不知道连接字符串可能是什么或在哪里输入它,因为 Aptana (v.3) 似乎缺乏我从 Eclipse 知道的旧数据源浏览器视图。有人能指出我正确的方向吗?

EDIT: I am working on linux - Mint 12

编辑:我正在使用 linux - Mint 12

回答by Gazler

You have neglected to mention the OS you are using.

您忽略了您正在使用的操作系统。

One way is to use the sqlite3command in your terminal.

一种方法是sqlite3在终端中使用该命令。

sqlite3 db/development.sqlite3

However, for things like inspecting your rows, you would be better using a rails console.

但是,对于检查行之类的事情,最好使用 rails 控制台。

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.

注意:不要直接通过 sqlite3 更改您的数据库架构,如果您来自不同的 Web 堆栈背景,您可能已经习惯了这一点。这是因为下次运行迁移时,状态将与 rails 预期的不同。

回答by Steve Weet

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 dbconsolewhich can be shortened to rails db

Rails 3 提供了一个通用命令来访问正确的数据库客户端,并为当前环境传入正确的数据库名称。此命令rails dbconsole可以缩短为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.

该命令仅提供 Gazler 的答案,实际上他对使用控制台的建议是很好的建议,但是此方法的优点是,如果您的数据库在其他环境中不同,它将使用正确的客户端。

回答by Tarun Gupta

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

回答by Dmitry Matveev

There is a great application to browse sqlite3 databases. SQLite Database Browser.

有一个很棒的应用程序可以浏览 sqlite3 数据库。SQLite 数据库浏览器

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 Textand never wanted to use anything else since, I advise you to check it out.

PS 您提到您正在使用 Aptana 工作室。我也用这个 IDE 开始了我的 RoR 学习,但后来发现了Sublime Text并且从来不想使用其他任何东西,我建议你检查一下。

Cheers

干杯

回答by oklas

You can have online access to your database if you use activeadmin.

如果您使用activeadmin.

Just add the gem activeadmin-sqlpage:

只需添加 gem activeadmin-sqlpage

gem 'activeadmin-sqlpage'

And create activeadmin page:

并创建 activeadmin 页面:

# 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+Enteror Submitbutton.

重新启动您的服务器。然后转到管理面板并导航菜单SQL。输入任何 sql 命令并按Ctrl+EnterSubmit按钮。

回答by supritshah1289

Open terminal and type this command. This will open a rails console to query the database.

打开终端并键入此命令。这将打开一个 rails 控制台来查询数据库。

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"]

例如:["schema_migrations", "ar_internal_metadata", "categories", "items"]

From the list of models, you can get first, last or all records.

从模型列表中,您可以获取第一个、最后一个或所有记录。

Category.all

回答by Zhenya

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.

如果您使用的是 RubyMine IDE,则可以从那里访问 sqllite 数据源。您可以使用 GUI 在数据库中运行查询或编辑数据。

回答by gsumk

To view data on DB, I used an SQLite client called DB Browser for SQLite,here is the link

要查看 DB 上的数据,我使用了一个名为 DB Browser for SQLite 的 SQLite 客户端,这是链接

There is a Linux version of this app too. There should be a database file with sqliteextension on dbdirectory of the app. On DB Browser, select Open Database option and choose that file and you should be able to view the data.

这个应用程序也有一个 Linux 版本。应用程序目录上应该有一个带有sqlite扩展名的数据库文件db。在 DB Browser 上,选择 Open Database 选项并选择该文件,您应该能够查看数据。