简体   繁体   中英

DBI/DBD::mysql/mysql_type_name: How to distinguish between Blob and Text?

This example outputs two times "blob". How could I find out, if the datatype of a column is TEXT ?

#!/usr/bin/env perl
use warnings;
use strict;
use DBI;
use Data::Dumper;

my$dbh = DBI->connect( "DBI:mysql:dbname=test", 'user', 'passwd', {
    RaiseError => 1,
    AutoCommit => 1,
} ) or die DBI->errstr;

my $table = 'my_test_table';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( Foo TEXT, Bar BLOB )" );

my $sth = $dbh->prepare( "INSERT INTO $table ( Foo, Bar ) VALUES( ?, ? )" );
$sth->execute( 'a', 'a' );

$sth = $dbh->prepare( "SELECT * FROM $table" );
$sth->execute();

my $col_types = $sth->{mysql_type_name};
print Dumper $col_types;

Output:

$VAR1 = [
        'blob',
        'blob'
        ];

Generally when you work with Database you know what the data fields are and you use fetch_hashref or fetch_arrayref functions and get the required data(in your case fields Foo(Text) and Bar(Blob)) , I didn't get what are you trying to achieve here? , If you need to get the data from Foo/Bar you should use fetch_arrayref or hashref functions, I also see that with 'use strict' you still managed to pass bareword 'mysql_type_name' to statement handle $sth.

This is because BLOB and TEXT are (nearly) the same thing in mysql: The BLOB and TEXT Types .

I am not sure why DBD::mysql can not distinguish the two. You may want to use VARCHAR instead of TEXT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM