Mojolicious 数据库插件 - 帮助程序错误
Mojolicious database plugin - helper error
我正在尝试使用Mojolicious database plugin,发现了一些困难。我已经访问了关于此主题的前两页 Google 结果。
场景非常简单:连接到数据库 (mysql),获取一些数据,显示它。
这是代码的摘录:
use Mojolicious::Lite;
use Mojolicious::Plugin::Database;
# [...]
sub startup {
my $self = shift;
$self->plugin('database', {
dsn => 'dbi:MySQL:dbname=y',
username => $mysql_user,
password => $mysql_passwd,
helper => 'ydb',
});
};
get '/' => sub {
my $c = shift;
my $zz=$c->ydb->prepare("select count(*) from customers");
my $total=$zz->execute();
$c->ydb->bind_columns($total);
$c->ydb->fetch;
$c->session(customer=>$total);
$c->render(template => 'index');
};
但我收到错误消息:
Can't locate object method "ydb" via package "Mojolicious::Controller"
我肯定缺少一些基础知识。有人能给我指出正确的方向吗?
我从来没有让插件的助手工作。我结合 Mojo::mysql.
创建了自己的助手
use Mojolicious::Lite;
use Mojo::mysql;
sub startup {
my $self = shift;
}
helper ydb => sub {
my $c = shift ;
return Mojo::mysql->strict_mode('mysql://muziek:muziek@localhost/muziek')
};
get '/foo' => sub {
my $c = shift ;
my $db = $c->ydb->db;
my $row = $db->query("select count(*) COUNT from TITLE")->hash;
$c->render(text => "Hello from /foo. count=" . $row->{COUNT} );
};
app->start;
我正在尝试使用Mojolicious database plugin,发现了一些困难。我已经访问了关于此主题的前两页 Google 结果。 场景非常简单:连接到数据库 (mysql),获取一些数据,显示它。 这是代码的摘录:
use Mojolicious::Lite;
use Mojolicious::Plugin::Database;
# [...]
sub startup {
my $self = shift;
$self->plugin('database', {
dsn => 'dbi:MySQL:dbname=y',
username => $mysql_user,
password => $mysql_passwd,
helper => 'ydb',
});
};
get '/' => sub {
my $c = shift;
my $zz=$c->ydb->prepare("select count(*) from customers");
my $total=$zz->execute();
$c->ydb->bind_columns($total);
$c->ydb->fetch;
$c->session(customer=>$total);
$c->render(template => 'index');
};
但我收到错误消息:
Can't locate object method "ydb" via package "Mojolicious::Controller"
我肯定缺少一些基础知识。有人能给我指出正确的方向吗?
我从来没有让插件的助手工作。我结合 Mojo::mysql.
创建了自己的助手use Mojolicious::Lite;
use Mojo::mysql;
sub startup {
my $self = shift;
}
helper ydb => sub {
my $c = shift ;
return Mojo::mysql->strict_mode('mysql://muziek:muziek@localhost/muziek')
};
get '/foo' => sub {
my $c = shift ;
my $db = $c->ydb->db;
my $row = $db->query("select count(*) COUNT from TITLE")->hash;
$c->render(text => "Hello from /foo. count=" . $row->{COUNT} );
};
app->start;