in reply to Using regex to add a single backslash to escape quotes, instead of 2 backslashes or 0

See the ->quote method of DBI. Do not implement this routine yourself, and even better, use DBI placeholders instead, also documented in DBI.

  • Comment on Re: Using regex to add a single backslash to escape quotes, instead of 2 backslashes or 0
  • Download Code

Replies are listed 'Best First'.
Re^2: Using regex to add a single backslash to escape quotes, instead of 2 backslashes or 0
by lancer (Scribe) on Jul 26, 2012 at 17:50 UTC
    Thanks for the suggestion, I was able to replace it with this:

    sub sql_escape { my $text = shift; $text = DBD::_::db->quote ($text); return $text; }

    Outputs: O''REILLY

    (This format works with MySQL)

      Why don't you use

      $dbh->quote(...)

      as the documentation suggests?

        I needed to convert a CSV file to SQL format (it would be later imported with phpMyAdmin). At the time of conversion there was no database to connect to, but a $dbh can be only created with a connection to a DB.

        Someone on StackOverflow suggested to use the above solution which works without a DB connection. ( http://stackoverflow.com/a/10458266 )