简体   繁体   中英

Rails import txt file into db

How can I rip these values into the DB tables?

First, I'll just put it out there, rather than letting you infer: I'm an ROR noob. I'm designing an app which populates the database--(SQLite) at this point--allows user to perform crud on these values, and exports the file in the same format as the original. This format is as such, and repeated through the document end. the ellipses represent the previous and next tones.

...

[Tone27]                            
Atone = 707.3                        
Btone = 746.8                        
Btonelength = 3                        
Btonedebounce = 1                    
Description = Fire Department 1                
mp3_Emails = email@address.com,email2@address.com,email3@address.com          
amr_Emails = email2@textmessaging.com,email1@textmessaging.com        
alert_command = c:\test.bat                
post_email_command = c:\test2.bat            
radio_frequency = 154.475    
exclude_from = 13:25                        
exclude_to = 13:35                        
exclude_emails = email2@textmessaging.com,email2@address.com

...

Now, the question is, in Rails, what is the easiest/most appropriate way to get these values into the db tables? I understand I must write a custom parser, but am not versed enough in ruby/rails to accomplish this efficiently.

Custom Parser

You need to read the file line by line and read out the attributes you want to save. It might look something like this:

file = File.new('example.txt', 'r')
while (line = file.gets)
  if line.match(/^\W[a-zA-Z0-9]*\W/) # practice your regex at rubular.com
    # create the object here
  end
  if line.match(/.*=.*/)
    pieces = line.split('=')
    key = pieces[0].strip
    value = pieces[1].strip
    # add this to the object here
  end
end

I'd suggest grabbing each line, trimming the whitespace (if any), and then splitting on the = sign, and then trimming again. There you have your key/value pair.

Every [Tone##] would be a new object, so you'd want to plan that into your loop or loops.

High Level Overview

First you should to design your database tables using migrations and create models to support those tables.

Next you need to parse the text files to create objects that have attributes and values of what you just parsed. This step is the purest ruby you will encounter (not much help at all from Rails here). This will require a custom parser to look in the text file for the particular values you want to pull out.

Once you have the objects created with the attributes and values parsed out of the text file, you will want to save all of those objects to the database.

I suggest building a sample or demo app before hammering away into this one. There are tutorials out there where you can have a simple app up and running within a couple hours at your experience level.

That's the high level overview of what you need to get done, each of those steps would most likely result in more specific StackOverflow questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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