我有一张桌子:
CREATE TABLE `sessions` (
id BINARY(16) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB;
我创建了一个值插入到
id
字段中:
$sql = 'INSERT INTO sessions (id) VALUES(?)';
my $generator = Data::UUID->new;
my $session_id = $generator->create();
$result = $dbh->do($sql, undef, $session_id);
根据
Data::UUID
模块的文档:
# creates binary (16 byte long binary value) UUID.
$ug->create();
https://metacpan.org/pod/Data::UUID
但是当我尝试插入数据时,我从 MariaDB 收到错误:
“id”列的数据太长
如果字段有 16 个字节长并且根据文档 $session_id 的大小也是 16 个字节,为什么它太长了?
(不是答案,评论太长)。
我没有遇到错误。
测试于
10.6.15-MariaDB-log
。
MariaDB [asterisk]> CREATE TABLE `sessions` (
-> id BINARY(16) NOT NULL,
-> PRIMARY KEY(id)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.052 sec)
MariaDB [asterisk]>
MariaDB [asterisk]>
MariaDB [asterisk]>
MariaDB [asterisk]> select * from sessions;
+------------------+
| id |
+------------------+
| ▒▒▒f▒▒▒▒▒\#z |
+------------------+
1 row in set (0.000 sec)
使用Perl脚本
use strict;
use warnings;
use DBI;
use Config::IniFiles;
use Data::UUID;
###################################################### DB connection ###########################################################
my $cfg = Config::IniFiles -> new( -file => '/etc/db.ini' );
my $dbinst = $cfg -> val('AST', 'DBINST');
my $dbhost = $cfg -> val('AST', 'DBHOST');
my $dbuser = $cfg -> val('AST', 'DBUSER');
my $dbpass = $cfg -> val('AST', 'DBPASS');
my $dbhA = DBI->connect( "dbi:mysql:$dbinst:$dbhost",$dbuser,$dbpass,
{ RaiseError => 0,
PrintError => 1,
AutoCommit => 0,
}
) or die $DBI::errstr;
###################################################### DB connection ###########################################################
my $generator = Data::UUID->new;
my $session_id = $generator->create();
my $query_insert_data = "INSERT INTO sessions ( id ) VALUES(?)";
my $statement_data = $dbhA->prepare($query_insert_data);
$statement_data->execute( $session_id );
$statement_data->finish();
################## Close DB connection ##################
warn $DBI::errstr if $DBI::err;
$dbhA->commit();
$dbhA->disconnect();
################## Close DB connection ##################