Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Dates and SQL

by ducky (Scribe)
on Sep 27, 2001 at 11:06 UTC ( [id://115012]=note: print w/replies, xml ) Need Help??


in reply to Dates and SQL

I've got a couple of suggestions for you:

  1. localtime? - Perhaps you might be interested in something you have a little more direct control over - Date::Format maybe?
  2. prepare? How about do - When you expect to get rows out of your SQL statement one option is to prepare a Statement Handle Object first. Since you're doing an UPDATE, you really shouldn't be getting any rows back. Try
    my $cur_time = time2str("%c", time) ; $dbh->do("UPDATE Schedules SET Date = ? WHERE ID = ?", undef, $cur_time, $id) ;
    Errors may be caught in $dbh->errstr
  3. Placeholders - In the 2nd item I use question marks as placeholders. The format for do is $dbh->do($statement, \%attr, @bind_values);. You'll probably not need attrs, so undef should suffice. Placeholders are great because they make it easy to read the SQL and DBI will do the escaping of the values for you.
  4. Using prepare - If you still would like to use prepare I'd recommended using placeholders:
    my $sth = $dbh->prepare("UPDATE Schedules SET Date = ? WHERE ID = ?"); my $cur_time = time2str("%c", time) ; $sth->execute($cur_time, $id) ;
    This may be prefered if you're going to be executing that $sth in a few places. Once $sth is prepared, it may be executed with different values multiple times.

Hope that helps =)

Update: BTW, I don't know what $id looks like, but if it's non-numeric, that's probably what UPDATE is barfing on: Needs escaping... but placeholders take care of that stuff =)

-Ducky

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found