Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Save and Load Data from MySQL DB

by kean (Sexton)
on Nov 03, 2011 at 13:57 UTC ( [id://935664]=perlquestion: print w/replies, xml ) Need Help??

kean has asked for the wisdom of the Perl Monks concerning the following question:

At the moment i load and save Userpictures (smaller 10kb) to the Harddisk. Now i want to save them to the MySQL DB. How can i save files (jpg) with DBD::mysql into a BLOB Field and load them into a Tk-Label?

Replies are listed 'Best First'.
Re: Save and Load Data from MySQL DB
by CountZero (Bishop) on Nov 03, 2011 at 17:08 UTC
    You cannot save a Tk-widget into a database: it is not an acceptable datatype for a database. If you want to save a widget, you should invent a suitable way of serializing the data in the widget, but in this case it would be easier and faster to just save the picture file itself.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Save and Load Data from MySQL DB
by stefbv (Curate) on Nov 03, 2011 at 16:33 UTC

    This code is from an old application, you have to experiment a little with it, it should work but not tested lately.

    Note that I used base64 encoding and the data was saved to a BLOB field of subtype text in a Firebird database.

    use MIME::Base64; ... #- New Label my $label = $pane->Label( -width => 646, -height => 486 )->pack; #- Default empty image my $jpgimg = $label->Photo( -format => 'jpeg', -file => '', ); $label->configure( -image => $jpgimg ); #- Process image open my $fh, '<', $img_file or die "Can't open file ", $img_file, ": $!"; binmode $fh; my $photo = do { local $/; <$fh> }; close $fh; my $stream = encode_base64($photo) or die $!; #- Load image in Label $label->blank; $label->configure( -format => 'jpeg', -data => $stream ); #- Retrieve value for DB insert my $value = $label->cget( -data );

    Regards, Stefan

Re: Save and Load Data from MySQL DB
by Anonymous Monk on Nov 03, 2011 at 14:01 UTC
      I tried this to save:
      use strict; use warnings; use Tk; use Tk::JPEG; use DBI; my $datenbank = "bilder"; my $db_host = "localhost"; my $db_port = "3306"; my $db_user = "root"; my $db_pw = "12345"; my $dbh; my $dsn = "DBI:mysql:database=$datenbank;host=$db_host;port=$db_port" +; eval { $dbh = DBI::->connect( $dsn, $db_user, $db_pw, { Raise +Error => 1, PrintError => 0, AutoCommit => 1 } ); }; if ($@) { print DBI::errstr; } my $mw = MainWindow->new(); my $bild = $mw->Photo( -file => "test.jpg" ); $dbh->do("INSERT INTO User (User,Bild) values (?,?)", undef, "2", $bil +d); my $label = $mw->Label(-image => $bild, -background => '#ffffff')->pac +k(); MainLoop;
      And this to read:
      use strict; use warnings; use Tk; use Tk::JPEG; use DBI; my $datenbank = "bilder"; my $db_host = "localhost"; my $db_port = "3306"; my $db_user = "root"; my $db_pw = "12345"; my $dbh; my $dsn = "DBI:mysql:database=$datenbank;host=$db_host;port=$db_port" +; $dbh = DBI::->connect( $dsn, $db_user, $db_pw, { RaiseError => 1, Prin +tError => 0, AutoCommit => 1 } ); my $mw = MainWindow->new(); my $sql = "SELECT Bild FROM User WHERE User = 2;"; my $sth = $dbh->prepare($sql); $sth->execute; my $bildausdb; ($bildausdb) = $sth->fetchrow_array; my $bild = $mw->Photo( -data => $bildausdb ); my $label = $mw->Label(-image => $bild, -background => '#ffffff')->pac +k(); MainLoop;

        $bild is a TK widget not your image data. Perhaps you need to store that data in the file 'test.jpg' instead?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://935664]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found