数据库列是否未签名?
Is a DB column UNSIGNED?
使用 Perl DBI,如何检查 DB table 的给定列是否未签名?
以下代码无法按预期工作(使用 MySQL 数据库):
my $sth = $dbh->column_info(undef, $schema, $Table, undef);
while (my $Column = $sth && $sth->fetchrow_hashref) {
my $ColumnName = $Column->{COLUMN_NAME};
my $data_type = $Column->{DATA_TYPE}; # SQL_DATA_TYPE does not work too
my $type_info = $dbh->type_info($data_type);
my $Unsigned = $type_info->{UNSIGNED_ATTRIBUTE};
my $UnsignedInfo = defined($Unsigned) ? ", unsigned=>$Unsigned" : "";
print $fh " '$ColumnName' => {type=>'$Column->{TYPE_NAME}', nullable=>$Column->{NULLABLE}$UnsignedInfo},\n";
}
如果该界面中没有任何本机内容,希望您可以使用 DBI 完成此操作:
create table myTable
( id int auto_increment primary key,
si int not null, -- signed
ui int unsigned not null -- unsigned
);
insert myTable (si,ui) values (1,1),(-7,8);
SELECT data_type,column_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema='so_gibberish' -- my database name
and table_name = 'myTable' AND COLUMN_NAME in ('si','ui');
+-----------+------------------+
| data_type | column_type |
+-----------+------------------+
| int | int(11) |
| int | int(10) unsigned |
+-----------+------------------+
使用 Perl DBI,如何检查 DB table 的给定列是否未签名?
以下代码无法按预期工作(使用 MySQL 数据库):
my $sth = $dbh->column_info(undef, $schema, $Table, undef);
while (my $Column = $sth && $sth->fetchrow_hashref) {
my $ColumnName = $Column->{COLUMN_NAME};
my $data_type = $Column->{DATA_TYPE}; # SQL_DATA_TYPE does not work too
my $type_info = $dbh->type_info($data_type);
my $Unsigned = $type_info->{UNSIGNED_ATTRIBUTE};
my $UnsignedInfo = defined($Unsigned) ? ", unsigned=>$Unsigned" : "";
print $fh " '$ColumnName' => {type=>'$Column->{TYPE_NAME}', nullable=>$Column->{NULLABLE}$UnsignedInfo},\n";
}
如果该界面中没有任何本机内容,希望您可以使用 DBI 完成此操作:
create table myTable
( id int auto_increment primary key,
si int not null, -- signed
ui int unsigned not null -- unsigned
);
insert myTable (si,ui) values (1,1),(-7,8);
SELECT data_type,column_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema='so_gibberish' -- my database name
and table_name = 'myTable' AND COLUMN_NAME in ('si','ui');
+-----------+------------------+
| data_type | column_type |
+-----------+------------------+
| int | int(11) |
| int | int(10) unsigned |
+-----------+------------------+