表格如下:
创建表格符号(
SymbolID整数不为空,
PngData BLOB
);
期待您的协助
答案 0 :(得分:0)
这是一种使用Perl可以实现的方法。首先,我将PNG图像加载到数据库中,然后将其提取到文件中以显示它是否有效。
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $myfile = "picture.png";
my $dbname = "test.db";
my $data;
# Connect to database
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname") || die "Cannot open db";
# >8 >8 >8 Cut starting here >8 >8 >8 >8 >8
# Open the image file
open MYFILE, $myfile or die "Cannot open file";
# Read in the file
while (<MYFILE>) {
$data .= $_;
}
close MYFILE;
# Insert image into database
my $sql = "INSERT INTO symbols VALUES (1,?)";
my $sth = $dbh->prepare($sql);
my $numrows = $sth->execute($data);
$sth->finish;
# >8 >8 >8 Cut ending here >8 >8 >8 >8 >8
#Now read it back and save as file
$sql = "SELECT PngData from symbols";
$sth = $dbh->prepare($sql);
$numrows = $sth->execute;
open OUTPUT, ">output.png";
my $ref = $sth->fetchrow_hashref;
my $newdata = $$ref{'PngData'};
print OUTPUT $newdata;
close OUTPUT;
$sth->finish;
$dbh->disconnect;
如果您只想从数据库中提取图像BLOB并将其另存为文件,请使用:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbname = "test.db";
my ($sql,$sth,$numrows,$ref,$newdata);
# Connect to database
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname") || die "Cannot open db";
# Read image and save as file
$sql = "SELECT PngData from symbols";
$sth = $dbh->prepare($sql);
$numrows = $sth->execute;
open OUTPUT, ">output.png";
$ref = $sth->fetchrow_hashref;
$newdata = $$ref{'PngData'};
print OUTPUT $newdata;
close OUTPUT;
$sth->finish;
$dbh->disconnect;
如果您想使用sqlite创建数据库,并使用此代码,请执行以下操作:
sqlite3 test.db "CREATE TABLE symbols(SymbolID integer,PngData BLOB);"