File: insertdb.pl

package info (click to toggle)
libdbix-class-perl 0.08123-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,520 kB
  • ctags: 1,695
  • sloc: perl: 19,821; sql: 353; makefile: 10
file content (59 lines) | stat: -rw-r--r-- 1,310 bytes parent folder | download
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
55
56
57
58
59
#!/usr/bin/perl -w

use MyDatabase::Main;
use strict;

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');

#  here's some of the sql that is going to be generated by the schema
#  INSERT INTO artist VALUES (NULL,'Michael Jackson');
#  INSERT INTO artist VALUES (NULL,'Eminem');

my @artists = (['Michael Jackson'], ['Eminem']);
$schema->populate('Artist', [
    [qw/name/],
    @artists,
]);

my %albums = (
    'Thriller' => 'Michael Jackson',
    'Bad' => 'Michael Jackson',
    'The Marshall Mathers LP' => 'Eminem',
);

my @cds;
foreach my $lp (keys %albums) {
    my $artist = $schema->resultset('Artist')->find({
        name => $albums{$lp}
    });
    push @cds, [$lp, $artist->id];
}

$schema->populate('Cd', [
    [qw/title artist/],
    @cds,
]);


my %tracks = (
    'Beat It'         => 'Thriller',
    'Billie Jean'     => 'Thriller',
    'Dirty Diana'     => 'Bad',
    'Smooth Criminal' => 'Bad',
    'Leave Me Alone'  => 'Bad',
    'Stan'            => 'The Marshall Mathers LP',
    'The Way I Am'    => 'The Marshall Mathers LP',
);

my @tracks;
foreach my $track (keys %tracks) {
    my $cd = $schema->resultset('Cd')->find({
        title => $tracks{$track},
    });
    push @tracks, [$cd->id, $track];
}

$schema->populate('Track',[
    [qw/cd title/],
    @tracks,
]);