导入SQL文件到Rails 4 ActiveRecord数据库? [英] Import SQL file to Rails 4 ActiveRecord db?

查看:53
本文介绍了导入SQL文件到Rails 4 ActiveRecord数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看了其他几个问题,它们有些相似,但与我要查找的不完全相同。

I've looked over several other questions on here, and they're vaguely similar, but not exactly what I'm looking for.

我要的是尝试做的是导入/转换一个* .sql文件,该文件包含8个表,每个表包含大约24列。该文件实际上是相当平坦的文件,似乎以前工作的唯一查询与在表之间关联共享的:id有关(因此,SELECT * FROM table1,table2 WHERE id ='1'会提取所有结果,而当时还可以)。

What I'm trying to do is import/"convert" a *.sql file which contains 8 tables, each of which contain roughly 24 columns. This file is actually fairly flat file, seeing as though the only queries that worked previous had to do with associating a shared :id between tables (so, SELECT * FROM table1, table2 WHERE id = '1' would pull all results, which was fine at the time).

我已经搜索过了,但是找不到合适的方法,所以现在我想请Rails专业人士提供帮助。

I've searched around, but can't find a clever way to do this, so I'm asking you Rails pros for help now.

推荐答案

如果我正确理解了您的问题,则需要从.sql文件中填充数据库。我是这样做的:

If I understood your question right you need to populate your db from .sql file. I am doing it this way:

connection = ActiveRecord::Base.connection

ql = File.read('db/some_sql_file.sql')
	statements = sql.split(/;$/)
	statements.pop
	ActiveRecord::Base.transaction do
		statements.each do |statement|
			connection.execute(statement)
		end
  end

将您的sql文件放入db文件夹。

Put your sql file to db folder.

这篇关于导入SQL文件到Rails 4 ActiveRecord数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆