Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Rose::DB::Object和MySQL设置空datetime_Mysql_Perl_Rose Db Object - Fatal编程技术网

使用Rose::DB::Object和MySQL设置空datetime

使用Rose::DB::Object和MySQL设置空datetime,mysql,perl,rose-db-object,Mysql,Perl,Rose Db Object,我可能错了,但这里似乎有冲突的标准 MySQL将存储的日期时间“0000-00-00:00:00”视为等同于NULL。 (如果datetime定义为NOTNULL,则似乎仅更新) 但是Rose::DB::Object将DateTime用于MySQL的DateTime字段,并且试图从“0000-00-00”设置空DateTime会在DateTime模块中引发异常。也就是说,我无法创建0年、0月、0天的DateTime对象,因为这会在DateTime模块中引发异常 我签入了Rose::DB::Obj

我可能错了,但这里似乎有冲突的标准

MySQL将存储的日期时间“0000-00-00:00:00”视为等同于NULL。 (如果datetime定义为NOTNULL,则似乎仅更新)

但是Rose::DB::Object将DateTime用于MySQL的DateTime字段,并且试图从“0000-00-00”设置空DateTime会在DateTime模块中引发异常。也就是说,我无法创建0年、0月、0天的DateTime对象,因为这会在DateTime模块中引发异常

我签入了Rose::DB::Object::Metadata::Column::Datetime,在创建条目或检索时看不到显式处理空Datetime的方法

我错过什么了吗

即使datetime(Perl模块)不能,Rose::DB::Object也可以处理空datetime(MySQL)字段

示例代码:

#!/usr/bin/perl
use strict;
use warnings;
use lib 'lib';
use RoseDB::dt_test;

my $dt_entry =  RoseDB::dt_test->new();
$dt_entry->date_time_field('0000-00-00');
$dt_entry->save;



1;

__END__
# definition of table as stored in DB

mysql> show create table dt_test \G
*************************** 1. row ***************************
       Table: dt_test
Create Table: CREATE TABLE `dt_test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `date_time_field` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
RoseDB::dt_测试模块为:

package RoseDB::dt_test;
use strict;
use warnings;

# this module builds up our DB connection and initializes the connection through:
# __PACKAGE__->register_db
use RoseDB;

use base qw(Rose::DB::Object);

__PACKAGE__->meta->setup (
    table => 'dt_test',

    columns =>
    [
      id              => { type => 'int', primary_key => 1 },
      date_time_field => { type => 'datetime' },
    ],
);

sub init_db { RoseDB->get_dbh }

1;
当我运行它时,我在tmp.pl第8行得到错误“无效日期时间:'0000-00-00”

当我将日期更改为“2010-01-01”时,它将按预期工作:

mysql> select * from dt_test\G
*************************** 1. row ***************************
             id: 1
date_time_field: 2010-01-01 00:00:00
我终于重新创建了空MySQL查询示例

mysql> create table dt_test(dt_test_field datetime not null);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into dt_test values(null);
ERROR 1048 (23000): Column 'dt_test_field' cannot be null
mysql> insert into dt_test values('0000-00-00');
Query OK, 1 row affected (0.00 sec)

mysql> select * from dt_test;
+---------------------+
| dt_test_field       |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from dt_test where dt_test_field is null;
+---------------------+
| dt_test_field       |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

看起来像是在表定义中,datetimes是用“notnull”定义的,然后试图使用MySQL的“假NULL”就是问题所在。我现在太累了,不能玩这个了,但我会看看早上更改表结构时会发生什么。

MySQL允许日期类型不完整(缺少年、月或日)。'0000-00-00'是一个有效的非空MySQL日期。为什么您认为它匹配为空

$ echo "select date('0000-00-00') is null" | mysql
date('0000-00-00') is null
0
作为比较:

$ echo "select date('0000-00-32') is null" | mysql
date('0000-00-32') is null
1

您应该能够将
datetime
列设置为所需的文本
0000-00-00 00:00:00
值,并将其保存到数据库:

$o->mycolumn('0000-00-00 00:00:00');
$o->save;
此类“全零”值不会被
Rose::DB::Object
转换为
DateTime
对象,而是保留为文本字符串。MySQL的
0000-00-00 00:00:00
DateTime字符串没有语义上的
DateTime
对象等价物

注意:
0000-00-00
是有效的
date
值,但
datetime
(或
timestamp
)值必须包括时间:
0000-00-00 00:00:00

要将列设置为null,请传递
unde
作为列值:

$o->mycolumn(undef);

当然,如果数据库中的列定义包含一个
notnull
约束,那么
save()
将不起作用。

我想您误解了-“从dial\u timestamp为NULL的表中选择dial\u timestamp”匹配dial\u timestamp(datetime)字段为“0000-00-00:00:00”的值。我想做的是使用Rose::DB::Object在某些DB表中获取和设置空日期-按照惯例,这是MySQL中的“0000-00-00:00:00”,你错了-至少在我使用过的任何版本的MySQL上都没有。试试看:
createtablefoo(bar-datetime)选择“0000-00-00:00:00”bar;选择*from foo,其中bar为空;选择*from foo,其中bar='0000-00-00:00:00';选择*,bar为空,bar='0000-00-00:00:00'from foo奇怪,我以前肯定是这样的,但现在我无法重现。嗯,我想我需要考虑一下。谢谢。成功复制(见上图)——看起来像MySQL的怪异。哦,太好了。我在5.0.32和5.0.75上也遇到了同样的情况:(一些示例代码可能会有所帮助。数据库的前后状态是什么,以及您使用什么Perl代码来尝试进行更改?因此,总而言之,这里有两个问题-(1)MySQL很奇怪,当datetime字段被定义为非空时,它被设置为“0000-00-00”。(2)我仍然无法通过Rose::DB::Object将datetime字段设置为NULL。我收到了错误:无效的datetime:“0000-00-00”位于/Library/Perl/5.8.8/Rose/Object.pm第25行(版本0.855)。此外,我发现我不能将字段留空然后保存,因为我一直得到一个“name\u of\u timestamp字段不能为NULL”错误,即使可能。如果这有意义:)