使用ruby脚本在MySQL中插入数据的最简单方法是什么?

时间:2023-01-27 16:37:49

I did a ruby script that parses a lot of files in ruby data structures, like hashes for example.

我做了一个ruby脚本,用于解析ruby数据结构中的大量文件,例如哈希。

I need to insert all this data in a MySQL database.

我需要在MySQL数据库中插入所有这些数据。

What I found:

我找到了什么:

mysql2

mysql2

tmtm

TMTM

dbi

DBI

Is there some native way to do this?

是否有一些本地方式来做到这一点?

Thanks for any help

谢谢你的帮助

EDIT

编辑

Lets say that I have a hash with 100 entries like this:

可以说我有一个包含100个条目的哈希:

hash = {"a" => 1, "b" => 2 ..., "c" => 100}

I would like to create a table at mysql with all this columns. I am afraid of Active Record is gonna be hard to do that.

我想在mysql上用所有这些列创建一个表。我害怕Active Record会很难做到这一点。

PS: Im not using Rails, just a simple ruby script

PS:我没有使用Rails,只是一个简单的ruby脚本

1 个解决方案

#1


3  

If I were you, I would prefer ActiveRecord, because I don't have to clutter my code with lots of SQL statements. Besides activerecord makes life easier.

如果我是你,我更喜欢ActiveRecord,因为我不需要用大量的SQL语句来混淆我的代码。除了activerecord使生活更轻松。

Set it up like this

像这样设置它

require 'active_record'

ActiveRecord::Base.establish_connection( 
 :adapter => "mysql2",
 :host => "host",
 :username=>"user",
 :password=>"user",
 :database => "your_db"
)

Then use tables like this

然后使用这样的表格

class SingularTableName < ActiveRecord::Base
  has_many :table_relationship
end

Then query like this

然后像这样查询

SingularTableName.all #=> all records
SingularTableName.first #=> first record
SingularTableName.where("query")
SingularTableName.create("...) #=> create a record/row

You can find more methods here => http://api.rubyonrails.org/classes/ActiveRecord/Base.html

你可以在这里找到更多方法=> http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Update:

更新:

To overcome plural table names and default primary key, you can use

要克服多个表名和默认主键,您可以使用

class AnyName < ActiveRecord::Base
  self.table_name = 'your table name'
  self.primary_key = 'your primary key'
  ...
end

#1


3  

If I were you, I would prefer ActiveRecord, because I don't have to clutter my code with lots of SQL statements. Besides activerecord makes life easier.

如果我是你,我更喜欢ActiveRecord,因为我不需要用大量的SQL语句来混淆我的代码。除了activerecord使生活更轻松。

Set it up like this

像这样设置它

require 'active_record'

ActiveRecord::Base.establish_connection( 
 :adapter => "mysql2",
 :host => "host",
 :username=>"user",
 :password=>"user",
 :database => "your_db"
)

Then use tables like this

然后使用这样的表格

class SingularTableName < ActiveRecord::Base
  has_many :table_relationship
end

Then query like this

然后像这样查询

SingularTableName.all #=> all records
SingularTableName.first #=> first record
SingularTableName.where("query")
SingularTableName.create("...) #=> create a record/row

You can find more methods here => http://api.rubyonrails.org/classes/ActiveRecord/Base.html

你可以在这里找到更多方法=> http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Update:

更新:

To overcome plural table names and default primary key, you can use

要克服多个表名和默认主键,您可以使用

class AnyName < ActiveRecord::Base
  self.table_name = 'your table name'
  self.primary_key = 'your primary key'
  ...
end