1

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???

3
  • Which database software? Commented Jan 23, 2011 at 14:07
  • "in text format" is vague. Now that I think about it, so is "in an automated manner" and "for further use in Rails". If you provided more details, you'd get better answers. Commented Jan 23, 2011 at 14:52
  • With "text format" I meant that the file is a .txt file and the contents include questions and answers which I can be parsed using regex. Commented Jan 24, 2011 at 14:18

1 Answer 1

3

I find Sequel to be faster than ActiveRecord, but both should be fine.

  1. Read your text file into memory. If it is too large to fit into memory, you'll want to use File.foreach to operate on it one line at a time.

  2. Parse your text file into an array of logical fields. I'd suggest using regular expressions, unless your text file is CSV.

  3. Perform a multi_insert on your database, which uses a single SQL command to insert n rows at once.

    • If you can't perform a batch insertion (I understand that ActiveRecord doesn't support it by default without some extension), then be sure at least to use a database transaction around all your inserts. If you don't, SQLite will be hitting the file system for each insert, which slows you down greatly.

Without more information about your "text file", this is about as helpful as I can be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.