Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Escaping text for MySQL

Escaping text for MySQL
Thread Tools
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 22, 2003, 09:17 AM
 
I have an AppleScript that enters data into a MySQL database. Its runs into problems when it tried to insert data that has characters that need to be escaped. I could write an AppleScript function to go through each string character by character, but I think that will slow it down SEVERELY. Are there any command line util's I can use via "Do shell script" that can escape text like I need? If you don't know, "Do Shell Script" lets you execute raw command lines through AppleScript.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Dec 22, 2003, 12:08 PM
 
Originally posted by l008com:
I have an AppleScript that enters data into a MySQL database. Its runs into problems when it tried to insert data that has characters that need to be escaped. I could write an AppleScript function to go through each string character by character, but I think that will slow it down SEVERELY. Are there any command line util's I can use via "Do shell script" that can escape text like I need? If you don't know, "Do Shell Script" lets you execute raw command lines through AppleScript.
I don't do much with AppleScript, but something like this should work:

Code:
echo 'hello, "bob", how are you?' | sed 's/"/\\"/g'
PS. I'd post this to the developers forum next time instead of UNIX.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
l008com  (op)
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status: Offline
Reply With Quote
Dec 22, 2003, 12:50 PM
 
that works for quotes but i need single quotes >'< and percent signs >%< and I think a few more for MySQL, but those three are the main ones. Thing is your code looks like someone leaned against their keyboard, so I don't really know how to add more types to it.
     
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status: Offline
Reply With Quote
Dec 22, 2003, 06:27 PM
 
Originally posted by l008com:
that works for quotes but i need single quotes >'< and percent signs >%< and I think a few more for MySQL, but those three are the main ones. Thing is your code looks like someone leaned against their keyboard, so I don't really know how to add more types to it.
You can easily change Arkham's example to more suit your needs... The code is a little mysterious, but check this out, all you need to pay attention to in that code is this part:

echo 'hello, "bob", how are you?' | sed 's/"/\\"/g'

The s/"/\\"/g tells sed to substitute all instances of the character after the "/" with the character after the next "/", which would be \\". The first backwards slash escapes the next backwards slash will get printed to the shell and passed on uninterpretted with the ". The "g" after the next "/" tells sed to do this substitution globally, and not just on the first instance that it finds.

Basically, this will replace all double quote characters with a \" so that by the time MySQL gets that string, it escapes the " and sees the double quote as exactly that.

I hope you understood that... it's kinda hard to denote on a message board... Just remember that any backwards slashes you see are escaping some character... if there are two then the code is escaping the backward slash itself... now you can mod Arkham's code as such to work with single quotes:
Code:
echo 'hello, "bob", how are you?' | sed 's/\'/\\\'/g'
I can imagine how confused you must be, I was pretty confused just typing this post.
"In Nomine Patris, Et Fili, Et Spiritus Sancti"

     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:27 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2