Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use strict;
- use warnings;
- use feature 'say';
- use Mojo::Pg;
- # Use a PostgreSQL connection string for configuration
- my $pg = Mojo::Pg->new('postgresql://localhost/testdb');
- $pg->migrations->name('my_names_app')->from_string(<<EOF)->migrate;
- -- 1 up
- create table names (id serial primary key, name text);
- -- 1 down
- drop table names;
- EOF
- # insert a name
- my $id = $pg->db->insert('names', { name => 'Fred Johnson' }, { returning => 'id' })->hash->{id};
- say "Insert id is $id";
- # update it, returning it
- say $pg->db->update('names', { name => 'Anderson Dawes' }, { id => $id }, { returning => 'name' })->hash->{name};
- ----
- Insert id is 16
- DBD::Pg::st fetchrow_hashref failed: no statement executing at /Users/justin/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Mojo/Pg/Results.pm line 22.
- ----
- $ DBI_TRACE=1 perl returning.pl 2> >(grep prepare)
- <- prepare_cached('select version from mojo_migrations where name = $1', HASH(0x7fb93621dad8), ...)= ( DBI::st=HASH(0x7fb9376db7d0) ) [1 items] at Database.pm line 86
- <- prepare_cached('INSERT INTO "names" ( "name") VALUES ( ? ) RETURNING "id"', HASH(0x7fb93621dad8), ...)= ( DBI::st=HASH(0x7fb9376dbfe0) ) [1 items] at Database.pm line 86
- Insert id is 17
- <- prepare_cached('UPDATE "names" SET "name" = ? WHERE ( "id" = ? )', HASH(0x7fb93621dad8), ...)= ( DBI::st=HASH(0x7fb9376e6920) ) [1 items] at Database.pm line 86
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement