# Problem with read file and write into the database

**URL:** <https://www.ruby-forum.com/t/problem-with-read-file-and-write-into-the-database/115779>\
**Category:** Rails\
**Created:** [September 28, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/problem-with-read-file-and-write-into-the-database/115779 "2007-09-28T14:14:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [September 28, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/problem-with-read-file-and-write-into-the-database/115779/1 "2007-09-28T14:14:37Z")

</div>

Hi All,

now i am facing one new problem, the problem is i have to check a  
specified directory for any number of files that the program will read  
and load into the database one file at a time. when the program finishes  
a file it should place the file in an archive directory. The program  
should also keep a log of it’s activity along with it;s errors.

so, for this i have created one directory called  
sampledb("#{RAILS\_ROOT}/sampledb"), inside the db contains many files,  
so if N number of files is there in the sense, i should read the file  
and write into the database, but i don’t know how to write the data into  
the database, so if any one knows, i need the idea how to implement the  
task, or please let me know any related site url, it would be great help  
for me, thanks in advance.

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [September 28, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/problem-with-read-file-and-write-into-the-database/115779/2 "2007-09-28T18:04:11Z")

</div>

logger = Logger.new(“loggfile.log”) # open a logger if you are not  
inside a controller  
begin  
dir = Dir.new(“#{RAILS\_ROOT}/sampledb”)  
dir.each { |dEntryName|  
file = File.new(dEntryName) #open the current entry in the  
directory  
next unless file.stat.file? # check is this a directory if yes  
ignore it.  
myDatabaseTable = MyTable.new() #pass argument to new if any  
extra columns present in table  
myDatabaseTable.file\_content = file.read # read all contents  
in file  
myDatabaseTable.save!  
}  
rescue Exception =\> e  
logger.error( e.message) #log what ever you want.  
return false # return if necessary or else forget it  
end

I guess this solves your problem and i assumed MyTable is table where  
your going to put your content and column name i assume it as  
file\_content,  
even you can save the file name inside the database, and (return false  
may not be necessary.) and also assuming you are using oracle db, and  
column type  
for file\_content as either varchar2, or blob or which ever is  
suitable.

Good luck,

On Sep 28, 5:14 pm, Vidya V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 1, 2007, 11:09am UTC](https://www.ruby-forum.com/t/problem-with-read-file-and-write-into-the-database/115779/3 "2007-10-01T11:09:00Z")

</div>

raghukumar wrote:

> logger = Logger.new(“loggfile.log”) # open a logger if you are not  
> inside a controller  
> begin  
> dir = Dir.new(“#{RAILS\_ROOT}/sampledb”)  
> dir.each { |dEntryName|  
> file = File.new(dEntryName) #open the current entry in the  
> directory  
> next unless file.stat.file? # check is this a directory if yes  
> ignore it.  
> myDatabaseTable = MyTable.new() #pass argument to new if any  
> extra columns present in table  
> myDatabaseTable.file\_content = file.read # read all contents  
> in file  
> myDatabaseTable.save!  
> }  
> rescue Exception =\> e  
> logger.error( e.message) #log what ever you want.  
> return false # return if necessary or else forget it  
> end
> 
> I guess this solves your problem and i assumed MyTable is table where  
> your going to put your content and column name i assume it as  
> file\_content,  
> even you can save the file name inside the database, and (return false  
> may not be necessary.) and also assuming you are using oracle db, and  
> column type  
> for file\_content as either varchar2, or blob or which ever is  
> suitable.
> 
> Good luck,
> 
> On Sep 28, 5:14 pm, Vidya V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

hi,

Thanks for your reply, and i tried this, its working nice, thank you  
very much to your idea
