質問

I have a bunch of configuration variables stored within a database, key value pairs accessable via the following query:

select * from conf_table;

I want to load these key/value pairs into a CGI::Applicaiton session. At the moment this is manually done (so not from database, but hardcoded) via

$self->session->param( NAME => VALUE );

For a bunch of key value pairs. Is there a more sensible way do to this with DBI and some form of loop?

Thanks

役に立ちましたか?

解決

You mean something like this?

my $sth = $dbh->prepare("select key, value from mytable");
$sth->execute;
$sth->bind_columns(\(my ($key, $value)));
while ($sth->fetch) {
  $self->session->param($key => $value);
}

他のヒント

DBI has some convenience methods that make this sort of work simpler. Try selectall_arrayref:

my $configs = $dbh->selectall_arrayref(
    'SELECT * FROM conf_table',
    { Slice => {} }, # make each row a hash
);
$self->session->param($_->{key} => $_->{value}) for @$configs;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top