Rails4 seeds.rb 与预先存在的 sql 文件

Rails4 seeds.rb with preexisting sql file

我有一个小的 seed.sql 文件,它由一系列 INSERT INTO 语句组成,例如 INSERT INTO posts (title, body) VALUES ("my post title", "the body of the post");

如何将此数据导入我的 Rails4 sqlite3 数据库?或将其导入我的 seed.rb 文件?

只需使用 SQLite 本身导入即可:

sqlite3 db/your_database.sqlite -init path/to/seeds.sql

已更新

或者,您可以打开 sqlite3 shell,然后从其中的文件读取任何 SQL 命令,首先 运行:

sqlite3 db/your_database.sqlite

然后在 sqlite shell 中,在 sqlite> 提示符下键入:

.read path/to/seeds.sql

这与 -init 选项的作用相同。