in reply to Re^2: Handle MySQL BIT data type in Perl
in thread Handle MySQL BIT data type in Perl

I know I'm 12 years late but I just had the same problem and it's still not magically solved in recent versions. However, I found another solution that might be interesting for someone like me who stumbles across this and has the same problem!

Similar to pg, I tried to insert a 1 or a 0 to a BIT(1) field in a MySQL table via DBI with placeholders. It did not work, same error. But I remembered that I previously had to convert the values from a query on said field that were returned by DBI (i.e. make "perl numbers" from the binary data):

my $perl_value = unpack("b", $dbi_returned_value);

Et voilá, the reverse (pack) worked fine with DBI, I didn't even needed to use bind_params()!

use strict; use warnings; use DBI; my $dbh = DBI->connect("DBI:mysql:database=Foo", "user", "password", { +mysql_enable_utf8 => 1, RaiseError => 1, AutoCommit => 0}) or die "Ca +nnot connect to database: $DBI::errstr"; my $statement = "insert into testtable(bitfield) values(?);"; my $insertnumber = pack("b",0); my $sth = $dbh->prepare($statement); $sth->execute($insertnumber); $dbh->commit; $dbh->disconnect;