Problem with read file and write into the database

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.

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. [email protected]

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. [email protected]

hi,

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