gpt4 book ai didi

Perl DBI 和 sql now()

转载 作者:行者123 更新时间:2023-12-01 10:05:53 25 4
gpt4 key购买 nike

我一直在尝试在更新表时使用 sql NOW() 函数。但是那个日期字段没有任何反应,我认为 DBI 只是忽略了那个值。代码是:

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

函数是:

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

如您所见,这是 sql 查询的“动态”生成,因此我不知道 sql 日期函数(如 now())来自何处。

在给定的示例中,sql 查询将是

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

带有参数值

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

但是日期栏还是显示0000-00-.......

有什么办法解决这个问题吗?

最佳答案

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.

不,它没有。但是当您实际上试图添加字符串 NOW() 时,DB 认为您提供的是日期时间或时间戳数据类型。那当然不行。不幸的是,DBI 无法为您找出答案。它所做的只是将 ? 更改为它获得的字符串的转义(通过 $dbh->quote())版本。

您可以做几件事:

  1. 以适当的格式提供当前时间戳字符串,而不是字符串 NOW()

    如果可用,可以使用 DateTime::Format::MySQL像这样:

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

    如果没有,就用DateTimeTime::Piece .

        use DateTime;
    my $now = DateTime->now->datetime;
    $now =~ y/T/ /;
    dbUpdate(
    $id,
    (notes => 'This was an update', filesize => '100.505', dateEnd => $now));
  2. 告诉您的 dbUpdate 函数查找类似 NOW() 的内容并做出相应的 react 。

    你可以这样做。但是有更好的方法来对此进行编码。您还应该考虑有例如CURRENT_TIMESTAMP() 也是如此。

    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. 自己写查询。

    您可能不会这样做,我不会添加示例,因为无论如何您都知道如何去做。


还有一些事情:这是您在程序运行时对数据库所做的唯一事情吗?每次进行查询时都连接和断开数据库是不明智的。最好在需要时连接数据库(或者在程序开始时,如果你总是使用它)并且在任何地方都使用这个 dbh/dbo。它节省了大量时间(和代码)。

关于Perl DBI 和 sql now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10833494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com