On Mon, Jan 07, 2002 at 01:13:50PM +0600, Alex Gerasev wrote:
> >
> > $sql_statement = "INSERT INTO rxPkt VALUES(\'$nodeId\', \'$rxNetTime\',
> > \'$rxLat\', \'$rxLon\', \'$rxHeading\', \'$antIndex\', \'$rxPower\',
> > \'$bin_msg\')";
>
> The best way to do this is use placeholders. In your example above, if any
> of the variables contains double quotes perl may interpret that as end of
> the string (and thus you'll have incorrect SQL statement). If variable
> contains single quote, MySQL will interpret that incorrectly.
A variable containing a double quote will not cause a problem in either
Perl or MySQL.
> Please try the following:
>
> $dbh->do(q{ INSERT INTO rxPkt VALUES (?, ?, ?, ?, ?, ?, ?, ?) }, undef,
> $nodeId, $rxNetTime, $rxLat, $rxLon, $rxHeading, $antIndex,
> $rxPower, $bin_msg
> );
>
> This is equivalent to:
> $sth = $dbh->prepare(q{ INSERT INTO rxPkt VALUES (?, ?, ?, ?, ?, ?, ?,
> ?) });
> $sth->execute($nodeId, $rxNetTime, $rxLat, $rxLon, $rxHeading, $antIndex,
> $rxPower, $bin_msg);
>
> Using placeholders is a common practice. It makes code more readable and
> helps you avoid errors.
>
> For more information on using placeholders, read "perldoc DBI".
Very good advice.
Ronald