Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:56:09+00:00 2026-06-04T17:56:09+00:00

I have been trying to use sql NOW() function while I update a table.

  • 0

I have been trying to use sql NOW() function while I update a table. But nothing happens to that date field, I think DBI just ignores that value. Code is :

dbUpdate($id, (notes => 'This was an update', filesize => '100.505', dateEnd => 'NOW()'));

and the function is :

sub dbUpdate {
    my $id = shift;
    my %val_hash = @_;
    my $table = 'backupjobs';

    my @fields = keys %val_hash;
    my @values = values %val_hash;

    my $update_stmt = "UPDATE $table SET ";
    my $count = 1;
    foreach ( @fields ) {
        $update_stmt .=  "$_ = ? ";
        $update_stmt .=  ', ' if ($count != scalar @fields);
        $count++;
    }
    $update_stmt .= " WHERE ID = ?";
    print "update_stmt is : $update_stmt\n";
    my $dbo = dbConnect();
    my $sth = $dbo->prepare( $update_stmt );
    $sth->execute( @values, $id ) or die "DB Update failed : $!\n";
    $dbo->disconnect || die "Failed to disconnect\n";
    return 1;
}#dbUpdate

As you can see this is a “dynamic” generation of the sql query and hence I dont know where an sql date function(like now()) come.

In the given example the sql query will be

UPDATE backupjobs SET filesize = ? , notes = ? , dateEnd = ?  WHERE ID = ?

with param values

100.55, This was an update, NOW(), 7

But the date column still shows 0000-00-……..

Any ideas how to fix this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:56:10+00:00Added an answer on June 4, 2026 at 5:56 pm

    I have been trying to use sql NOW() function while I update a table. But
    nothing happens to that date field, I think DBI just ignores that
    value.

    No, it doesn’t. But the DB thinks you are supplying a datetime or timestamp data type when you are in fact trying to add the string NOW(). That of course doesn’t work. Unfortunately DBI cannot find that out for you. All it does is change ? into an escaped (via $dbh->quote()) version of the string it got.

    There are several things you could do:

    1. Supply a current timestamp string in the appropriate format instead of the string NOW().

      If you have it available, you can use DateTime::Format::MySQL like this:

      use DateTime::Format::MySQL;
      dbUpdate(
        $id,
        (
          notes => 'This was an update',
          filesize => '100.505',
          dateEnd => DateTime::Format::MySQL->format_datetime(DateTime->now),
        )
      );
      

      If not, just use DateTime or Time::Piece.

          use DateTime;
          my $now = DateTime->now->datetime;
          $now =~ y/T/ /;
          dbUpdate(
            $id,
            (notes => 'This was an update', filesize => '100.505', dateEnd => $now));
      
    2. Tell your dbUpdate function to look for things like NOW() and react accordingly.

      You can do something like this. But there are better ways to code this. You should also consider that there is e.g. CURRENT_TIMESTAMP() as well.

      sub dbUpdate {
          my $id = shift;
          my %val_hash = @_;
          my $table = 'backupjobs';
      
          my $update_stmt = "UPDATE $table SET ";
      
          # Check for the NOW() value
          # This could be done with others too
          foreach ( keys %val_hash ) {
            if ($val_hash{$_} =~ m/^NOW\(\)$/i) {
              $update_stmt .= "$_ = NOW()";
              $update_stmt .= ', ' if scalar keys %val_hash > 1;
              delete $val_hash{$_};
            }
          }
      
          # Put everything together, join keeps track of the trailing comma
          $update_stmt .= join(', ', map { "$_=?" } keys %val_hash );
          $update_stmt .= " WHERE ID = ?";
          say "update_stmt is : $update_stmt";
          say "values are: ", join(', ', values %val_hash);
      
          my $dbo = dbConnect();
          my $sth = $dbo->prepare( $update_stmt );
          $sth->execute( values %val_hash, $id ) or die "DB Update failed : $!\n";
          $dbo->disconnect || die "Failed to disconnect\n";
          return 1;
      }
      
    3. Write your queries yourself.

      You’re probably not going to do it and I’ll not add an example since you know how to do it anyway.


    Here’s something else: Is this the only thing you do with your database while your program runs? It is not wise to connect and disconnect the database every time you make a query. It would be better for performance to connect the database once you need it (or at the beginning of the program, if you always use it) and just use this dbh/dbo everywhere. It saves a lot of time (and code).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have been trying to use Launchpad's API to write a small
I have been trying to use report viewer for a couple of days now
I have been trying to use date_format , however as far as I know,
I have been trying to use array_unique to remove duplicates from the search results
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have been trying to use/save the boolean value of a checkbox in other
I have been trying to use 3rd party tool called visiblox for rendering charts
I have been trying to use the document.getElementByID to pull information from an HTML
Ok I'm stumped. I have been trying to use Simpletip to create a tooltip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.