in reply to Re: dbi mysql concat interpolation
in thread dbi mysql concat interpolation

Thanks.

my $sth = $dbh->prepare("SELECT CONCAT('My','''', 'QL')");

It is work.

Replies are listed 'Best First'.
Re^3: dbi mysql concat interpolation
by Don Coyote (Hermit) on Feb 20, 2015 at 11:43 UTC

    It is unsafe.

    Hello lykich and welcome to the monastery.

    Binding to placeholders '?' increases prevention of execution of malformed queries. Each '?' is replaced by the value relative to position in the execute statement.

    Firstly, construct your statements.

    #!perl; use strict; use warnings; use 5.10.1; # features my @quotes = ( q/'/, q/"/ ); push @quotes, map q/'/, 0 .. int(rand(10)); push @quotes, map q/"/, 0 .. int(rand(10)); say 'quotes ', join( ' ', @quotes); my @Various_Apostrophes = map { $_ .= ( int(rand(40)+2) % 2 == 1 ) ? q!'! : q!"! ; } @quotes; say 'V_A ', join( ' ', @Various_Apostrophes); my $scquery = join('', q/'SELECT CONCAT( /, q/ ?,/ x (scalar(@Various_Apostrophes)-1), q/ ? )'/ ); say 'scq ',$scquery;

    Secondly, supply the preconstructed prepare and execute variables to the database methods.

    my $sth = $dbh->prepare( $scquery ); $sth->execute( @Various_Apostrophes ); exit 0;

    In this way you separate code that handles query statements from code handling database requests. This makes safer the script and quickly debugs.


    cp1252:chr(14): You cannot optimise code you have not yet written.