Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

C:DBI fails to update DB?

by zigdon (Deacon)
on Sep 28, 2004 at 13:28 UTC ( #394570=perlquestion: print w/replies, xml ) Need Help??

zigdon has asked for the wisdom of the Perl Monks concerning the following question:

This is probably something really silly, but a quick ping on the ChatterBox didn't show up anything obvious.

I'm trying to use C:DBI to update a field in the database. Here's the outline of the code:

my $groupid = ...; my $newvalue = ...; foreach my $key (@somekeys) { my ($obj) = MyCDBI::Obj->search(groupid => $groupid, userid => $key) +; if (defined $obj) { if ($newvalue eq 'somevalue') { $obj->status(lc $newvalue); } elsif ($newvalue eq 'someothervalue') { $obj->status("something"); } elsif ... ... } print "new value: ", $obj->status; $obj->update; } else { print "Not found"; } }
Now, the code runs, and prints the new value properly. But looking in the database, the new value doesn't get saved.

The definitions: (each in it's own file)

package MyCDBI::DBI; use base 'Class::DBI'; __PACKAGE__->set_db('Main', 'dbi:mysql:cdbidb', 'user', 'pass'); 1; package MyCDBI::Obj; use base 'MyCDBI::DBI'; use Apache::Reload; __PACKAGE__->table('objs'); __PACKAGE__->columns(Primary => qw/userid orgid status/); __PACKAGE__->has_a(userid => "MyCDBI::User"); __PACKAGE__->has_a(orgid => "MyCDBI::Org"); 1;
Other updates (elsewhere in the code, in other pages), do work properly. I'm wondering if this has something to do with the scoping of $obj?

Update: Not sure if this is related, but the 'status' column is an 'enum'. The values I'm trying to assign are valid.

Any hints or suggestions would be very welcome!

-- zigdon

Replies are listed 'Best First'.
Re: C:DBI fails to update DB?
by perrin (Chancellor) on Sep 28, 2004 at 21:22 UTC
    What kind of tables are you using? You aren't doing a commit. That's okay with MyISAM, but not with InnoDB.

      I am using MyISAM though. Another piece of information I can't seem to understand: I do two prints, one before the modifcation, one after. The first does show the current value of the field, the second prints an empty string. I know that after an update the object is reloaded from the database, but it should autopopulate the field when I access it, right?

      Any ideas where else I can look? I'm trying to think of different ways of getting the update through, but until I find one, I'm stuck dead in the water.

      Thanks!

      -- zigdon

Re: SOLVED: C:DBI fails to update DB?
by zigdon (Deacon) on Sep 30, 2004 at 13:00 UTC
    The problem was that all the columns were declared as "Primary" instead of just the first two. Thanks to William McKee on the cdbi-talk list for pointing that out!

    -- zigdon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://394570]
Approved by mpeppler
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2019-05-20 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (123 votes). Check out past polls.

    Notices?