Hi all,

I'd like to know if it is possible to create a perl script that would take the names of image files with its extension of say 10,000 images from out of a folder and place them into a database table in separate rows to use as sample data?

Thank you

The following script creates a table (if it doesn't already exist) and saves all the image names (extension included) and the directory names.

#!/usr/bin/perl
use strict; 
use warnings; 
use DBI;
use File::Glob ':glob';
use File::Basename;

my $dbh=DBI->connect('dbi:mysql:daniweb','david','dogfood') ||
   die "Error opening database: $DBI::errstr\n";

$dbh->do("CREATE TABLE IF NOT EXISTS images (id INT NOT NULL AUTO_INCREMENT,
                                i_name VARCHAR(64),
                                i_dir VARCHAR(100),
                                PRIMARY KEY (id))");

my $sth = $dbh->prepare("insert into `images` (i_name, i_dir) values (?, ?)")
             or die "prepare failed: " . $dbh->errstr();  


my @files = </home/david/Pictures/*>;
foreach (@files) {
    my($filename, $dir) = fileparse($_);
    $sth->execute($filename, $dir) or die "execute failed: " . $sth->errstr(); 
}

# Close connection
$sth->finish();
undef($dbh);
Comments
Nice one
This question has already been answered. Start a new discussion instead.