Re: Updating lots of database fields in a single row

2013-01-24 Thread William Blunn
On 22/01/2013 22:57, Andrew Beverley wrote: Basically, I'd like to update lots of fields in a single database row. From the subject line I thought you were going to ask how to update lots of fields in lots of rows in a single query... Considering a table "table" with primary key columns "pkc

Re: Updating lots of database fields in a single row

2013-01-24 Thread Jonathan McKeown
On Jan 24, 2013 7:32 PM, "Greg McCarroll" wrote: > Perl was never cool, it was the kid that liked wearing a sports coat > to school with leather patches sewn on the elbows > as they took style tips from their 'cool' maths teacher. Oi, I resemble that remark! /me wonders what happened to that coa

Re: Updating lots of database fields in a single row

2013-01-24 Thread Greg McCarroll
On 24 Jan 2013, at 17:15, Paul Makepeace wrote: So we're using Perl still then? :D Perl was never cool, it was the kid that liked wearing a sports coat to school with leather patches sewn on the elbows as they took style tips from their 'cool' maths teacher. G.

Re: Updating lots of database fields in a single row

2013-01-24 Thread Paul Makepeace
On Jan 24, 2013 8:48 AM, "Greg McCarroll" wrote: > > > > No no no, lets just use any language that our process analyst consultant decides - they can come up with a long winded approach to software development that will ensure the lack of any possible security holes by providing long winded documen

Re: Updating lots of database fields in a single row

2013-01-24 Thread Greg McCarroll
No no no, lets just use any language that our process analyst consultant decides - they can come up with a long winded approach to software development that will ensure the lack of any possible security holes by providing long winded documentation to auditors and by selecting whatever language

Re: Updating lots of database fields in a single row

2013-01-24 Thread Dirk Koopman
On 24/01/13 16:00, David Cantrell wrote: On Thu, Jan 24, 2013 at 04:49:17PM +0100, Abigail wrote: On Thu, Jan 24, 2013 at 03:38:08PM +, Greg McCarroll wrote: Tied variables ;-) Overloaded constants, and not even your place holders are safe. Stupid language. Let's all use C instead. No

Re: Updating lots of database fields in a single row

2013-01-24 Thread Joseph Werner
Why C? Assembly is so much more concise. On Thu, Jan 24, 2013 at 11:00 AM, David Cantrell wrote: > On Thu, Jan 24, 2013 at 04:49:17PM +0100, Abigail wrote: >> On Thu, Jan 24, 2013 at 03:38:08PM +, Greg McCarroll wrote: >> > Tied variables ;-) >> Overloaded constants, and not even your place h

Re: Updating lots of database fields in a single row

2013-01-24 Thread David Cantrell
On Thu, Jan 24, 2013 at 04:49:17PM +0100, Abigail wrote: > On Thu, Jan 24, 2013 at 03:38:08PM +, Greg McCarroll wrote: > > Tied variables ;-) > Overloaded constants, and not even your place holders are safe. Stupid language. Let's all use C instead. -- David Cantrell | Cake Smuggler Extraord

Re: Updating lots of database fields in a single row

2013-01-24 Thread Abigail
On Thu, Jan 24, 2013 at 03:38:08PM +, Greg McCarroll wrote: > > Tied variables ;-) Overloaded constants, and not even your place holders are safe. Abigail

Re: Updating lots of database fields in a single row

2013-01-24 Thread Greg McCarroll
Tied variables ;-) G. On 24 Jan 2013, at 13:57, Denny wrote: > You do know what $status contains in the example (so you could hardcore it in > the SQL anyway). $id is up for grabs though. > > > > Simon Wilcox wrote: > >> On 24/01/2013 03:01, Sam Kington wrote: >>> I mean, sure, this is s

Re: Updating lots of database fields in a single row

2013-01-24 Thread Denny
You do know what $status contains in the example (so you could hardcore it in the SQL anyway). $id is up for grabs though. Simon Wilcox wrote: >On 24/01/2013 03:01, Sam Kington wrote: >> I mean, sure, this is safe: >> >> if ($status eq 'foo') { >> $dbh->do("UPDATE table SET status='$sta

Re: Updating lots of database fields in a single row

2013-01-24 Thread Simon Wilcox
On 24/01/2013 03:01, Sam Kington wrote: I mean, sure, this is safe: if ($status eq 'foo') { $dbh->do("UPDATE table SET status='$status' WHERE id=$id"); } Only if you're certain you know what $status and $id contain. http://xkcd.com/327/

Re: Updating lots of database fields in a single row

2013-01-23 Thread Sam Kington
On 23 Jan 2013, at 11:09, Abigail wrote: > I'd say that dogmas are poor practise. > > > Good practise is actually *knowing* when you should use placeholders, > and when there's no need. > > Because someone who knows can actually be trusted to do variable > interpolation in places where placeho

Re: Updating lots of database fields in a single row

2013-01-23 Thread Andrew Beverley
On Tue, 2013-01-22 at 22:57 +, Andrew Beverley wrote: > I've not been developing with Perl for long, so I'd like to know if > there is a better way of writing the following database query (or is > there a better place to ask?): Thanks for the many excellent replies. I'll have a play around wit

Etiquette [was: Re: Updating lots of database fields in a single row]

2013-01-23 Thread Gordon Banner
Be nice as this list has more to offer than silly pounding on people that release things onto the CPAN or send unclear emails. A person asks a question. We give an answer. We can only say so much. A person can only take on board so much. So we have to make that answer as good as possible in t

Re: Updating lots of database fields in a single row

2013-01-23 Thread Chris Jack
> On 3//1//013 0::1,, J?r?me ?t?v? wrote: > > Something critical is missing in your code though: quoting: > > > > Replace $field = '$hash->{$field}' with " $field > > =".$dbh->quote($hash->{$field}) This would assume all fields were strings. To do it properly, you would need to have the metadat

Re: Updating lots of database fields in a single row

2013-01-23 Thread William Blunn
On 23/01/2013 11:09, Abigail wrote: On Wed, Jan 23, 2013 at 10:53:16AM +, William Blunn wrote: On 23/01/2013 10:21, Jérôme Étévé wrote: Something critical is missing in your code though: quoting: Replace $field = '$hash->{$field}' with " $field =".$dbh->quote($hash->{$field}) The DBI quot

Re: Updating lots of database fields in a single row

2013-01-23 Thread Abigail
On Wed, Jan 23, 2013 at 10:53:16AM +, William Blunn wrote: > On 23/01/2013 10:21, Jérôme Étévé wrote: >> Something critical is missing in your code though: quoting: >> >> Replace $field = '$hash->{$field}' with " $field >> =".$dbh->quote($hash->{$field}) >> >> The DBI quote method will 'do th

Re: Updating lots of database fields in a single row

2013-01-23 Thread William Blunn
On 23/01/2013 10:21, Jérôme Étévé wrote: Something critical is missing in your code though: quoting: Replace $field = '$hash->{$field}' with " $field =".$dbh->quote($hash->{$field}) The DBI quote method will 'do the right thing to avoid screwing up your queries'. http://search.cpan.org/dist/

Re: Updating lots of database fields in a single row

2013-01-23 Thread Jérôme Étévé
On 22 January 2013 22:57, Andrew Beverley wrote: > I've not been developing with Perl for long, so I'd like to know if > there is a better way of writing the following database query > my @fields = qw(field1 field2 field3 field4 field5 field6 field7 ... ); > my @updates; > foreach my $field (@fie

Re: Updating lots of database fields in a single row

2013-01-23 Thread Abigail
On Tue, Jan 22, 2013 at 10:57:29PM +, Andrew Beverley wrote: > I've not been developing with Perl for long, so I'd like to know if > there is a better way of writing the following database query (or is > there a better place to ask?): > > > my @fields = qw(field1 field2 field3 field4 field5 f

Re: Updating lots of database fields in a single row

2013-01-23 Thread William Blunn
On 23/01/2013 09:52, Mark Overmeer wrote: * Ruud H.G. van Tol (rv...@isolution.nl) [130123 09:45]: On 2013-01-23 10:27, William Blunn wrote: my @fields_to_update = grep { $hash->{$_} } @fields; Be aware that it skips any false value, like undef, '', '0', 0. my @fields_to_update = grep defined

Re: Updating lots of database fields in a single row

2013-01-23 Thread William Blunn
On 23/01/2013 09:40, Ruud H.G. van Tol wrote: On 2013-01-23 10:27, William Blunn wrote: my @fields_to_update = grep { $hash->{$_} } @fields; Be aware that it skips any false value, like undef, '', '0', 0. Absolutely. I wouldn't usually do a straight truth test on a scalar value myself, un

Re: Updating lots of database fields in a single row

2013-01-23 Thread Mark Overmeer
* Ruud H.G. van Tol (rv...@isolution.nl) [130123 09:45]: > On 2013-01-23 10:27, William Blunn wrote: > > >my @fields_to_update = grep { $hash->{$_} } @fields; > > Be aware that it skips any false value, like undef, '', '0', 0. my @fields_to_update = grep defined $hash->{$_}, @fields; But that

Re: Updating lots of database fields in a single row

2013-01-23 Thread Ruud H.G. van Tol
On 2013-01-23 10:27, William Blunn wrote: my @fields_to_update = grep { $hash->{$_} } @fields; Be aware that it skips any false value, like undef, '', '0', 0. -- Ruud

Re: Updating lots of database fields in a single row

2013-01-23 Thread William Blunn
On 22/01/2013 22:57, Andrew Beverley wrote: I've not been developing with Perl for long, so I'd like to know if there is a better way of writing the following database query (or is there a better place to ask?): my @fields = qw(field1 field2 field3 field4 field5 field6 field7 ... ); my @updat

Re: Updating lots of database fields in a single row

2013-01-22 Thread Mark Fowler
On Tuesday, January 22, 2013, Philip Skinner wrote: my $qry = $self->dbh->prepare("UPDATE table SET " . join (sort(@fields), > '=?, ') . " WHERE id=?"); > my $affected = $qry->execute(@hash{sort(keys %hash)}, $id); I know this is just demoing how to use hash manipulation to get two matched lists

Updating lots of database fields in a single row

2013-01-22 Thread Mark Fowler
On Tuesday, January 22, 2013, Andrew Beverley wrote: is a better way of writing the following database query With Perl there's always more than one way to do it. Greg's post rightly recommended ORMs and pointed out that, indeed, DBIx::Class is commonly considered the current best of breed for g

Re: Updating lots of database fields in a single row

2013-01-22 Thread Greg McCarroll
In this day and age I'd be looking at an ORM[1] layer for such simple changes, they are almost foolproof until someone is a fool ;-). And they will probably avoid stupid SQL mistakes that you and I might both make. And DBIx::Class[2] is the current best of breed, it can also 'reverse engineer'

Re: Updating lots of database fields in a single row

2013-01-22 Thread Philip Skinner
Maybe something like: my $qry = $self->dbh->prepare("UPDATE table SET " . join (sort(@fields), '=?, ') . " WHERE id=?"); my $affected = $qry->execute(@hash{sort(keys %hash)}, $id); Though I've had quite a bit to drink. On 01/22/2013 10:57 PM, Andrew Beverley wrote: I've not been developing w

Updating lots of database fields in a single row

2013-01-22 Thread Andrew Beverley
I've not been developing with Perl for long, so I'd like to know if there is a better way of writing the following database query (or is there a better place to ask?): my @fields = qw(field1 field2 field3 field4 field5 field6 field7 ... ); my @updates; foreach my $field (@fields) { push @upda