Escaping apostrophies in AIR / SQLite
I ran into a problem with AIR SQLite where a user is allowed to enter text that is saved to the database. If they add an apostrophe to the text then the SQL query will fail silently.
All it takes to fix the problem is replace each apostrophe with two apostrophes. It must be something to do with the old Buddhist saying, “One apostrophe makes a bug. Two apostrophes make one apostrophe.”
Here’s the code:
var textThatMayHaveApostrophe:String = someInput.text;
var regEx:RegExp = /'/g; //'
textThatMayHaveApostrophe = textThatMayHaveApostrophe.replace( regEx, "''" );
The //’ comment following the RegEx declaration is there so that Flex Builder will apply code coloring correctly below that line. Otherwise it thinks everything below that line is part of a String. Even though it still compiles just fine.
If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.






