Forum: Ruby noob question:traverse directory and populate mysql db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
De60a566d8e2a8dec790d1f6741ad424?d=identicon&s=25 jay (Guest)
on 2007-02-01 10:52
Hi,

i was hoping for a little bit of direction on a project i'm starting
that will look to scan a directory structure full of jpgs, and populate
a db table with various meta data (i.e. filename, path, maybe some exif
data if i can do that easily).

it's clear that i'll need find.find(), 'mysql' and a few other nifties.
it's more about how to get the data into the db.

would you loop through the find and create a hash for each file, then a
hash of hashes for all of them, before mapping it into the db?

any comments or pointers gratefully recieved.
thx

jay
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 Simon Kröger (Guest)
on 2007-02-01 20:35
(Received via mailing list)
jay schrieb:
> would you loop through the find and create a hash for each file, then a
> hash of hashes for all of them, before mapping it into the db?
>
> any comments or pointers gratefully recieved.
> thx
>
> jay

Hi jay,

this isn't optimal but should get you started:

----------------------------------------------------------------------
require 'mysql'

db = Mysql::new("localhost", "user", "pw", "testdb")
db.query("CREATE TABLE IF NOT EXISTS jpgs (name text, size int)")

Dir['/path/to/images/**/*.jpg'].each do |file|
   db.query("INSERT INTO jpgs VALUES ('#{file}',  #{File.size(file)})")
end

db.close
----------------------------------------------------------------------

if my memory doesn't play tricks on me it should be *way* faster
to use a single insert statement with lots of data - have fun!

cheers

Simon
This topic is locked and can not be replied to.