-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.pl
54 lines (39 loc) · 1021 Bytes
/
webapp.pl
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
45
46
47
48
49
50
51
52
53
54
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
$self->render(text => 'Hello World');
};
get 'book' => sub {
my $self = shift;
use DBIx::Connector;
use utf8;
# 接続の作成
my $dsn = 'dbi:SQLite:dbname=test.db';
my $conn = DBIx::Connector->new($dsn, undef, undef, {
RaiseError => 1,
PrintError => 0,
AutoCommit => 1
});
# データベースハンドルの取得
my $dbh = $conn->dbh;
# ステートメントハンドルの準備
my $sth = $dbh->prepare('select * from book where title like ?');
# SQLの実行
my @params = ('%Perl%');
$sth->execute(@params);
# レコードの取得
my $texts = '';
my @texts;
while (my $row = $sth->fetchrow_hashref) {
my $id = $row->{id};
my $title = $row->{title};
my $text = "id: $id, title: $title";
use Encode 'decode';
$text = decode('UTF-8', $text);
$texts .= $text . " <br />";
push @texts, $text;
}
# response
$self->render(text => $texts);
};
app->start;