I’m writing a .sql file using Perl, not writing to the mysql database directly. I need to quote the strings in that file. Looks like the $dbh-quote() method does what I want. Except that I can’t figure out how to use it without a database connection. I’d make a dummy connection, but I can’t seem to do this without a database running, and the machine that is going to run this cron job won’t have one.
This question is similar: Perl DBI without accessing the database and the accepted answer states that “You might attempt to load the driver you need and call the function directly”, which is what I would like to do, but I can’t figure out how to do that.
It sounds like the answer is a resounding “you can’t do it”. In any case here is a quote function I whipped up according to the mysql documentation http://dev.mysql.com/doc/refman/5.0/en/string-literals.html about what needs to be quoted.
My input is UTF-8, perl stores strings in UTF-8 internally, my output is UTF-8, and my database is UTF-8. Given that the character set is consistent, I shouldn’t need to worry to much about it for escaping purposes.
I tested this function and it appears to do what I want. Too bad something like this isn’t accessible from a library where it is already written.