Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

class-dbi-mysql and null values

by davidj (Priest)
on Nov 05, 2006 at 07:35 UTC ( #582289=perlquestion: print w/replies, xml ) Need Help??
davidj has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone,
I've just started using class-dbi for some work stuff and I'm still trying to get used to some of its functionality. The problem I'm having is getting a column value updated to a NULL value.

In the webapp update form, when a value from a particular field is removed, I need the corresponding value for that field value in the database to be set to NULL. I'm updating the value as per class-dbi:
$case->owner( $q->param('owner') ); ..... $case->update;
However, this sets the value to an empty, non-null value which will cause a search on NULL to fail. If I put NULL as a value in the code, I get the bareword error:
$case->owner(NULL); ..... $case->update;
I've made the following changes in the class file to hack a workaround:
Handoff::Cases->set_sql('unowned', qq{SELECT * FROM cases WHERE owner IS NULL
Handoff::Cases->set_sql('unowned', qq{SELECT * FROM cases WHERE owner IS NULL OR owner=''
This hack will make it work for my application, but it breaks other applications that require a NULL value in the field since the field does not, in fact, have a NULL value. There's got to be a way to do it. I just don't know what it is.

As always your assistance is much appreciated.

Replies are listed 'Best First'.
Re: class-dbi-mysql and null values
by davidj (Priest) on Nov 05, 2006 at 12:04 UTC
    I found a resolution. Its actually quite simple: just test for the length of the owner parameter and explicity asign undef if the length is 0.
    if (length($owner) > 0) { $case->owner( $q->param('owner') ); } else { $case->owner( undef ); }
    Thanks for you assistance,
Re: class-dbi-mysql and null values
by tinita (Parson) on Nov 05, 2006 at 11:01 UTC
    $case->owner( $q->param('owner') );
      $case->owner( scalar $q->param('owner') );
    because param() returns the empty list (in list context) if a parameter isn't defined.
      I thought about doing something like that, also; to get an undefined value in there. However, even if $q->param('owner') is empty, it is still defined.
Re: class-dbi-mysql and null values
by fmerges (Chaplain) on Nov 05, 2006 at 11:36 UTC


    Don't do that, don't use a param directly to update the DB. There's something called tainting and validation. Take a look at Data::FormValidator.


    fmerges at
      I'm familiar with taintedness and validation. I cut that part out of the code I supplied since it didn't seem relevant to the specific problem. Good catch, though.
Re: class-dbi-mysql and null values
by Anonymous Monk on Nov 05, 2006 at 09:08 UTC
    ...I get the bareword error
    NULL is not a perl keyword. Try $case->owner('NULL');
      Yeah, when I tried  $case->owner(NULL) I quickly realized why it didn't work. I also tried  $case->owner('NULL') and it sets he value of the field to the string NULL.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://582289]
Approved by friedo
[davido]: ok, on my ubuntu system getlogin grabs from /proc/self/ loginuid (per strace)
[tye]: disable /proc and then see what it does?
[davido]: then it reads from /etc/passwd to decide who my uid is.
[davido]: sorry, typed that before you asked me to disable proc
[davido]: but you stumped me; don't know how to disable proc.
[tye]: I don't know if you can just dismount, but I thought so.
[davido]: yeah, umount -f isn't powerful enough.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-06-23 20:03 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.