Saving data from text file into database

Hi i'm new to ROR. I do not sure how am i going to extract out the data
from a text file into a database.

This is the asd.txt file i want to extract:

6598766293@mimail.com.sg,Fri May 23 16:43:28 +0800 2008,River Valley
High School Address: Malan Road 10 Singapore Bukit Merah Singapore
Contact: Phone: +6564741709 Category: Educational institute

I already created the column field in database as
MSISDN is for storing the first element
datetime is for storing the second element
description is for stiring the third element

Please help. Thanks

Emma,

You refer to three elements, so I take it you are treating the file as
comma separated value (CSV) file.

you can do something like:

def self.load_csv_file
   FasterCSV.foreach("filename") do |row|
       s= Student.new(:MSISDN=>row[0],
                              :datetime=>row[1],
                              :description=>row[2]
                              )
       s.save
    end
    end
end

Assuming your model is called student.rb.
Warning! it is not a good idea to call a fieldname datetime since
this is a data type, use something like "entry_time". Also it is best
not to use capitals for fieldnames, since capitals have specific
meaning to ruby code.
Note: FasterCSV is a plugin, or you can use standard ruby CSV in a
similar way.
hth
Tonypm

Tonypm Thanks a lot for your help.

I have solved the previous problem. Now i facing the date format in my
database. I am using mysql as my database then the datetime format is
2008-06-04 15:17:55. But my datetime format is like this Fri May 23
16:43:28 +0800 2008. So when i execute it say incorrect datetime value.
how do i change the date_format or should i change the datatype to
string instead.

Actually i generate a scaffold in rails like these:

ruby script/generate scaffold MSISDN:string datetime:datetime
description:text

and after that i rake the migrate folder and it generate a schema.rb.

Now how do i change the schema.rb attribute.

2008-06-04 15:17:55. But my datetime format is like this Fri May 23
16:43:28 +0800 2008. So when i execute it say incorrect datetime value.
how do i change the date_format or should i change the datatype to
string instead.

http://www.ruby-doc.org/core/classes/Date.html
http://www.ruby-doc.org/core/classes/Time.html
are the doc pages you want.

You can use Date.parse("Fri May 23 16:43:28 +0800 2008") to get a date
object
from a string or Date.parse("Fri May 23 16:43:28 +0800 2008") to get a
Time object.

Now how do i change the schema.rb attribute.

You can run db:migrate with a version number (or use db:rollback) to
get back to the version prior to the table creation, and then edit the
migration file to change what you require, then run db:migrate again.
However, the field names in the scaffold wont have changed so you will
have to run scaffold generate again or change the individual field
names in the views. I am not the best person to advise on scaffold
though because I rarely use it, I tend to build my own files using
templates that suit my page structure. In fact although I havn't
done it in practice, I guess you could db:rollback and then run
scaffold generate again.

Tonypm

Thanks anyway i have solved the problem with just this code that matched
mysql datetime format

  date = ((DateTime.parse(datetime)).strftime(fmt='%Y-%m-%e %T') )

Thanks anyway

Wawa