I have a single text file with data in text format and I want to put this data in an automated manner in a database for further use in a Ruby on Rails application and I want to use Ruby for solving this problem.
I want to know the most efficient way of solving this problem and the possible solutions, that come in your mind.
The solutions I have found include using the active record to fetch the data from the file and into the database which might not be the most efficient, so let’s see what you have got in your mind???
I find Sequel to be faster than ActiveRecord, but both should be fine.
Read your text file into memory. If it is too large to fit into memory, you’ll want to use
File.foreachto operate on it one line at a time.Parse your text file into an array of logical fields. I’d suggest using regular expressions, unless your text file is CSV.
Perform a
multi_inserton your database, which uses a single SQL command to insert n rows at once.Without more information about your “text file”, this is about as helpful as I can be.