perl5-dbi / DBD-MariaDB

Perl MariaDB driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unable to get return value from MariaDB via perl DBI

ibksoftware opened this issue · comments

I am testing the JSON data type in a MariaDB database. Here is the table create statement

CREATE TABLE `vs` (
`AutoID` INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`ShowID` VARCHAR(25) NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
`ClientID` VARCHAR(25) NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
`Categories` LONGTEXT NULL DEFAULT '0' COLLATE 'utf8mb4_bin',
`Attr` LONGTEXT NULL DEFAULT '0' COLLATE 'utf8mb4_bin',
`UUID` VARCHAR(50) NULL DEFAULT '0' COLLATE 'utf8mb4_unicode_ci',
PRIMARY KEY (`AutoID`),
UNIQUE INDEX `VSUUID` (`UUID`),
INDEX `VS-Show` (`AutoID`, `ShowID`, `ClientID`),
INDEX `VS-clientID` (`ClientID`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
;

Here is the sample row data

INSERT INTO `vs` (`AutoID`, `ShowID`, `ClientID`, `Categories`, `Attr`, `UUID`) VALUES (0000000001, 'Demo', 'NEA', '{"buttonfmt": "red", "categories": ["Parts", "Paint / PBE / Refinishing", "OEM / Auto Manufacturer", "Tools & Equipment", "Education / Trade Organization / Consulting", "Mechanical Repair", "Other"]}', '0', 'c41ac5cb-d900-4991-9c3e-0b710fded735');

When I execute the following perl code I do not see any data for the result column

use strict;
use DBI;
use Data::Dumper;

our $SQL;
our $sth='';
my $UUID;
my $rv;

my $dbh = DBI->connect("DBI:MariaDB:IBKREG:localhost",
                       'UserID', 'Password',
                       { RaiseError => 1, PrintError => 0 });

$SQL="select Showid, JSON_EXTRACT(categories,'$.buttonfmt') as result from ibkreg.vs where ClientID='NEA';";  
$sth  = $dbh->prepare($SQL);
$sth->execute();
while (my $hr = $sth->fetchrow_hashref) {
    print Dumper($hr);
}
exit;

The following is the output from Data::Dumper

$VAR1 = {
'Showid' => 'Demo',
'result' => undef
};

When I run the SQL statement in Heidi SQL I get "RED" in the Result field as I should. I have updated DBI and the DBD::mysql and DBD::MariaDB modules. Am I missing something?

I have tested this in Python3 and I get the data i expect.

Try escaping $ character in:

"select Showid, JSON_EXTRACT(categories,'$.buttonfmt') as result from ibkreg.vs where ClientID='NEA';"

That was it. Not what what I expected

You don't need double quotes here, so you can use the q() operator:

$SQL = q(select Showid, JSON_EXTRACT(categories,'$.buttonfmt') as result from ibkreg.vs where ClientID='NEA');  

@ibksoftware @choroba Could you close this issue?