1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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, { RaiseError => 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, "1", $bild); my $label = $mw->Label(-image => $bild, -background => '#ffffff')->pack(); MainLoop;
1 2 3
my $sth = $dbh->prepare( qq{SELECT Bild FROM User WHERE User=?} ); $sth->execute($Username); ($bild) = $sth->fetchrow_array;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!perl use strict; use warnings; use Tk; use Tk::PNG; use Tk::JPEG; use DBI; my $datenbank = "test"; my $db_host = "localhost"; my $db_port = "3306"; my $db_user = "test"; my $db_pw = "test"; my $dsn = "DBI:mysql:database=$datenbank;host=$db_host;port=$db_port"; my $dbh = DBI->connect( $dsn, $db_user, $db_pw, { RaiseError => 1, PrintError => 0, AutoCommit => 1 } ) or die('Err connect: ' . DBI->errstr()); my $mw = Tk::MainWindow->new(); #my $filename = "test.jpg"; my $filename = "apply.png"; my $bild = $mw->Photo( -file => $filename ); # -- Bild in Variable einlesen open MYFILE, $filename or die "Cannot open file: $!"; binmode MYFILE; my $data; while (<MYFILE>) { $data .= $_; } close MYFILE; # ------------------------- my $sql = q~INSERT INTO photos (photo) values (?)~; my $sth = $dbh->prepare($sql) or die('Error prep: ' . $dbh->errstr()); $sth->bind_param(1, $data); my $rv = $sth->execute($data) or die('Error exec: ' . $dbh->errstr()); my $label = $mw->Label(-image => $bild, -background => '#ffffff')->pack(); $mw->MainLoop();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!perl # cf. http://www.james.rcpt.to/programs/mysql/blob/ use strict; use warnings; use DBI; use Tk; use Tk::PNG; use Tk::JPEG; use MIME::Base64 qw(encode_base64); # Which database and which file from the filesystem? my $dbname = 'test'; my $username = 'test'; my $password = 'test'; # Make a connection to your database my $dbh = DBI->connect("dbi:mysql:database=$dbname", $username, $password) or die "Cannot open db"; my $sql = "SELECT photo FROM photos LIMIT 1"; my $sth = $dbh->prepare($sql) or die('Error prep: ' . $dbh->errstr()); my $numrows = $sth->execute() or die('Error exec: ' . $dbh->errstr()); my $ref = $sth->fetchrow_hashref; my $newdata = $$ref{'photo'}; # siehe: http://www.perl-community.de/bat/poard/thread/5210 $newdata = encode_base64($newdata); my $mw = Tk::MainWindow->new(); my $bild = $mw->Photo( -data => $newdata ); my $label = $mw->Label(-image => $bild, -background => '#ffffff')->pack(); $mw->MainLoop();