简体   繁体   English

如何将哈希值放入sqlite3数据库?

[英]How can I put a hash into a sqlite3 database?

I've got several hashes stored in an array in memory, like: 我在内存中的数组中存储了多个哈希,例如:

ruby-1.9.3-p0 :020 > grok.tracks[0]
=> {:track_id=>1391, :name=>"Becoming a Better Developer", :artist=>"Ruby Rogues", :album=>"Ruby Rogues", :genre=>"Podcast", :kind=>"MPEG audio file", :size=>110164706, :total_time=>4596271, :year=>2011, :date_modified=>"2011-08-29T04:49:04", :date_added=>"2011-08-29T04:49:04", :bit_rate=>192, :sample_rate=>44100, :release_date=>"2011-08-25T17:25:43", :persistent_id=>"6E564B7750B54D85", :track_type=>"File", :podcast=>true, :unplayed=>true, :location=>"file://localhost/Users/lunks/Music/iTunes/iTunes%20Media/Podcasts/Ruby%20Rogues/Becoming%20a%20Better%20Developer.mp3", :file_folder_count=>4, :library_folder_count=>1} 
ruby-1.9.3-p0 :021 > grok.tracks.size
=> 14152 

And I want to save them on an SQLite3 database. 我想将它们保存在SQLite3数据库中。 My database already has the table and all the columns I need. 我的数据库已经有了该表和我需要的所有列。 The database is named Library.sqlite3 and the table is tracks . 该数据库名为Library.sqlite3 ,该表为tracks How can I put all the hashes in the Sqlite3 database? 如何将所有哈希放入Sqlite3数据库中?

require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => "sqlite",
  :database  => "path/to/Library.sqlite3"
)

class Track < ActiveRecord::Base
end

# read hashes from wherever you have them
# grok = Grok.read_tracks

for track_hash in grok.tracks
  track = Track.new(track_hash)
  track.save
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM