use warnings;
use strict;
use feature 'say';
use DBI;
my $dbh = DBI->connect(
"DBI:mysql:database=testing;host=127.0.0.1", $ENV{USER},
'barfoo', { RaiseError => 1, AutoCommit => 1 });
$dbh->do('DROP TABLE IF EXISTS texts');
$dbh->do(<<'ENDSQL');
CREATE TABLE texts (
string VARCHAR(256)
);
ENDSQL
$dbh->do('INSERT INTO texts (string) VALUES ("/a/b/c");');
# ---
my $string = '/a/b/c';
my $req = qq{ SELECT * FROM `texts` WHERE `string`=?};
my $sth = $dbh->prepare($req);
my $row = $sth->execute($string);
say "row = $row";
# ---
my $sth2 = $dbh->prepare(
q{ SELECT * FROM texts WHERE string LIKE '%/a/b/c%' });
$sth2->execute;
say "row = $row";
This prints row = 1 twice, as expected.
This is how I spun up a fresh test MySQL instance. To change the server's port number, change the -p3306:3306 to something like -p13306:3306 in the docker command, add the argument --port=13306 to the mysql command, and add the argument ;port=13306 to the DBI connection string in the Perl code.
$ docker run --rm --name testmysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=foobar -d mysql:5
# wait a few seconds for it to start
$ echo "CREATE USER '$USER' IDENTIFIED BY 'barfoo'; CREATE DATABASE testing; GRANT ALL PRIVILEGES ON testing.* TO '$USER';" | mysql --protocol=TCP --user=root --password=foobar
# after testing:
$ docker stop testmysql
Several smaller edits to add more information. |