Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

DBI recipes

by gmax (Abbot)
on Aug 17, 2003 at 18:47 UTC ( [id://284436]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    sub table_exists {
        my $db = shift;
    ...
        print "table not found!\n";
        $dbh->do( $create_query );
    }
    
  2. or download this
        my $create_query = qq{
            create table employees (
    ...
        my $fieldlist = join ", ", @fields;
        my $field_placeholders = join ", ", map {'?'} @fields;
    
  3. or download this
        my $insert_query = qq{
            INSERT INTO employees ( $fieldlist )
    ...
        my $sth= $dbh->prepare( $insert_query );
    # $insert_query = "INSERT INTO employees ( id, name, salary )
    #            VALUES ( ?, ?, ? )";
    
  4. or download this
        my ($id,  $name, $salary)  = (5, 'Sal', 4500);
        $sth->execute($id, $name, $salary);
    
  5. or download this
        while ( get_values($id, $name, $salary) {
            $sth->execute($id, $name, $salary);
        }
    
  6. or download this
        my @employees_lol = (
            [1, 'Fred',   5000],
    ...
        for (@employees_lol) {
            $sth->execute(@$_);
        }
    
  7. or download this
        my @employees_loh = (
            {id => 3, name => 'Kim',  salary =>  5600},
    ...
        for (@employees_loh) {
            $sth->execute($_->{id}, $_->{name}, $_->{salary});
        }
    
  8. or download this
        for (@employees_loh) {
            $sth->execute(@{$_}{@fields})
        }
    
  9. or download this
        my $query1 = qq{select name from employees};
        my $sth= $dbh->prepare($query1);
    ...
        while (my ($name) = $sth->fetchrow_array) {
            push @names, $name;
        }
    
  10. or download this
        my $query2 = qq{select id, name, salary from employees};
        my @names = map {$_->[1]}
            @{$dbh->selectall_arrayref($query2)};
    __END__
    @names = (qw(Fred Joshua Kim Dave Sal));
    
  11. or download this
        #
        # either
    ...
    __END__
    $names_ref = ['Fred', 'Joshua', 'Kim', 'Dave', 'Sal'];
    @names = ('Fred', 'Joshua', 'Kim', 'Dave', 'Sal');
    
  12. or download this
        my $sth = $dbh->prepare($query2);
        $sth->execute();
    ...
            @{$sth->fetchall_arrayref([0,-2])};
    __END__
    @names = (qw(Fred Joshua Kim Dave Sal));
    
  13. or download this
        #
        # either
    ...
      [ '4', 'Dave', '6000' ],
      [ '5', 'Sal', '4500' ]
    ];
    
  14. or download this
        my $sth = $dbh->prepare($query2);
        $sth->execute();
    ...
      { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
      { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
    ];
    
  15. or download this
        my $employees_loh = $dbh->selectall_arrayref($query2, {Slice => {}
    +});
    
  16. or download this
        my $sth = $dbh->prepare($query2);
        $sth->execute();
    ...
    
      { 'salary' => '4500', 'name' => 'Sal' }
    ];
    
  17. or download this
        my %employees_h =
            map { $_->[1], $_->[2]}
    ...
        'Dave' => '6000' ,
        'Sal' => '4500'
        );
    
  18. or download this
        #
        # either
    ...
      '4' => [ 'Dave', '6000' ],
      '5' => [ 'Sal', '4500' ]
    );
    
  19. or download this
        my $employees_hoh = $dbh->selectall_hashref($query2, 1);
    __END__
    ...
     '4' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
     '5' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
    };
    
  20. or download this
        my $employees_hoh2 = $dbh->selectall_hashref($query2, 3);
    __END__
    ...
      '4500' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' },
      '6000' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' }
     };
    
  21. or download this
        my ($id, $name, $salary) ;
    
    ...
    3       Kim         5600
    4       Dave    6000
    5       Sal         4500
    
  22. or download this
        $sth->execute;
    
    ...
        $sth->bind_columns ( \( $id, $name, $salary ) );
    
        print "$id\t$name\t$salary\n" while $sth->fetchrow_arrayref;
    
  23. or download this
        my @empl = (undef, undef, undef);
        $sth->execute;
    ...
        $sth->bind_columns ( \( @empl ) );    
    
        print join( "\t", @empl), $/ while $sth->fetchrow_arrayref;
    
  24. or download this
        @empl = ( \$id, \$name, \$salary );
        $sth->execute;
    
        $sth->bind_columns( @empl );
        print "$id\t$name\t$salary\n" while $sth->fetch;
    
  25. or download this
        my %rec = (
            emp_id => undef,
    ...
              "$rec{first_name}\t",
              "$rec{monthly_payment}\n"
                    while $sth->fetchrow_arrayref;
    
  26. or download this
        my @fields = (qw(emp_id first_name monthly_payment));
    
    ...
              "$rec{first_name}\t",
              "$rec{monthly_payment}\n"
            while $sth->fetchrow_arrayref;
    
  27. or download this
    
        use Benchmark (qw(cmpthese));
    ...
    fr_hashref  3858/s        --           -56%        -59%
    man_hashref 8681/s      125%             --         -8%
    fr_arrayref 9416/s      144%             8%          --
    
  28. or download this
        $sth = $dbh->prepare(qq{
            SELECT
    ...
        # using a arrayref
        # columns => id,id
        # 1 1
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://284436]
Approved by larsen
Front-paged by valdez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-16 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found