Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: dbi style questions (code, discussion)

by uwevoelker (Pilgrim)
on Dec 29, 2001 at 23:30 UTC ( #135149=note: print w/replies, xml ) Need Help??


in reply to dbi style questions (code, discussion)

At first I would like to thank markjugg for the tip with DBIx::Abstract.

I usually use the other INSERT syntax (similar to UPDATE, with SET).

Here comes an example:
sub db_store_event { my $id = shift || 0; my $ret = $id; if ($id > 0) { # update my $hash = db_get_event($id); my $event = impand_event(); # konvertiere Uhrzeit und Datum my @update = (); foreach my $key (keys %$hash) { if (defined $event->{$key} and ($hash->{$key} ne $event->{ +$key})) { push @update, $key.'='.$dbh->quote($event->{$key}); } } if (scalar @update > 0) { db_do("UPDATE ".$config->{event_table}." SET ".join(', ', +@update)." WHERE id=$id"); } } else { # insert my $event = impand_event(); # konvertiere Uhrzeit und Datum my @dat = (); foreach my $key (keys %$event) { push @dat, $key.'='.$dbh->quote($event->{$key}) if $event- +>{$key}; } my ($err, $sth) = db_sth("INSERT INTO ".$config->{event_table} +." SET ".join(', ', @dat)); $ret = $sth->{'mysql_insertid'} or die "no db_id"; } return $ret; }
The routine impand_event() konverts the cgi parameter to an hashref with keys like the fields in the database and does some conversion (for date and time).
sub impand_event { my ($datum, $zeit); my %event = (); foreach my $key ($cgi->param) { if ($key =~ m/event\.(\w+)/) { $event{$1} = $cgi->param($key); + } } $datum = $event{datum_beginn}; if ($datum) { $event{datum_beginn} = date_to_sql($datum); } $datum = $event{datum_ende}; if ($datum) { $event{datum_ende} = date_to_sql($datum); } $zeit = $event{zeit_beginn}; if ($zeit) { $event{zeit_beginn} = time_to_sql($zeit); } $zeit = $event{zeit_ende}; if ($zeit) { $event{zeit_ende} = time_to_sql($zeit); } return wantarray ? %event : \%event; }
Sorry for the german comments, but these are real lines of code.

uwevoelker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://135149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-05-23 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?