在rails3中的seed.rb内执行sql脚本

发布于 2024-11-11 17:38:18 字数 463 浏览 0 评论 0原文

我想在我的seed.rb中执行这个sql脚本

LOAD DATA LOCAL INFILE '/home/list-38.csv'
INTO TABLE list
FIELDS TERMINATED BY ':'
LINES TERMINATED BY '\n'
(email,name,password);

我检查了这个链接但无法找出解决方案。所以一旦我们运行 rake db:seed

如何通过在 Ruby Rails 平台中运行 sql 脚本来为 mysql 数据库做种? 它转储我的数据写入表中。

有任何疑问..回复

谢谢

I want to execute this sql script inside my seed.rb

LOAD DATA LOCAL INFILE '/home/list-38.csv'
INTO TABLE list
FIELDS TERMINATED BY ':'
LINES TERMINATED BY '\n'
(email,name,password);

I checked this link but unable to figure out the solution.So that once we run rake db:seed

How to seed mysql database by running sql scripts in Ruby Rails platform? it dumps my data into the table.

any queries ..do reply

Thanx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甩你一脸翔 2024-11-18 17:38:18

db/seeds.rb中尝试使用rake db:seed执行原始SQL

connection = ActiveRecord::Base.connection()
connection.execute("*_YOUR_SQL_HERE_*")

Try this in db/seeds.rb to execute raw SQL with rake db:seed

connection = ActiveRecord::Base.connection()
connection.execute("*_YOUR_SQL_HERE_*")
太阳哥哥 2024-11-18 17:38:18

对于多个 sql 语句,我最终分别迭代每个语句:

# db/seeds.rb

connection = ActiveRecord::Base.connection()

sql = <<-EOL
  INSERT INTO users values ('Admin');
  INSERT INTO categories values ('Supervisors');
EOL

sql.split(';').each do |s|
  connection.execute(s.strip) unless s.strip.empty?
end

For multiple sql statements, I ended up iterating over each statement separately:

# db/seeds.rb

connection = ActiveRecord::Base.connection()

sql = <<-EOL
  INSERT INTO users values ('Admin');
  INSERT INTO categories values ('Supervisors');
EOL

sql.split(';').each do |s|
  connection.execute(s.strip) unless s.strip.empty?
end
假情假意假温柔 2024-11-18 17:38:18

Fredrik Boström 的答案的一个变体可以从文件加载:

# db/seeds.rb

def execute_sql_file(path, connection = ActiveRecord::Base.connection)
  require 'active_support/core_ext/object/blank.rb'
  IO.read(path).split(';').reject(&:blank?).each do |statement|
    connection.execute(statement)
  end
end

execute_sql_file('my.sql')

A variation on Fredrik Boström's answer that can load from a file:

# db/seeds.rb

def execute_sql_file(path, connection = ActiveRecord::Base.connection)
  require 'active_support/core_ext/object/blank.rb'
  IO.read(path).split(';').reject(&:blank?).each do |statement|
    connection.execute(statement)
  end
end

execute_sql_file('my.sql')
路弥 2024-11-18 17:38:18

这个答案已经很晚了,但希望它对处于我位置的人有所帮助。您可以使用 .execute_batch 直接连接到您正在使用的数据库版本,而不是迭代每个 sql 语句。下面是 SQLite3 的示例。

#db/seeds.rb

DB = {:conn => SQLite3::Database.new("./db/development.sqlite")}

sql = <<-EOL
  INSERT INTO users values ('Admin');
  INSERT INTO categories values ('Supervisors');
EOL

DB[:conn].execute_batch(sql)

Super late to this answer but hopefully it helps someone in my position. Instead of iterating over each sql statement you can use .execute_batch using a direct connection to your database with the database version you are using. Below is an example with SQLite3.

#db/seeds.rb

DB = {:conn => SQLite3::Database.new("./db/development.sqlite")}

sql = <<-EOL
  INSERT INTO users values ('Admin');
  INSERT INTO categories values ('Supervisors');
EOL

DB[:conn].execute_batch(sql)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文