views:

81

answers:

3

I have updated many records already, but when it came to a word that contains a quote I get this error: "ERROR: Unclosed quote @ 1357"

I know why it's giving me this error, I just don't how to solve it.

Here's a sample: UPDATE invnum SET cAccountName = replace(cAccountName,'JOHN'S','BEN')

Thanks in advance.

+2  A: 

Escape quotes inside strings:

UPDATE invnum SET cAccountName = replace(cAccountName,'JOHN\'S','BEN')

You want to be really careful with this - not dealing with this properly is the source of SQL injection attacks, and is a major source of security problems.

Dominic Rodger
A: 

Try this instead:

UPDATE invnum SET cAccountName = replace(cAccountName,"JOHN'S","BEN")

If you need to use both types of quotes within a string, then you'll need to escape the type of quotes you use to surround the string when they occur within it (otherwise the SQL interpreter will think the string ends before it actually does.

For instance:

Johns   becomes "Johns"
John's  becomes "John's" or 'John\'s'
"John"  becomes '"John"' or "\"John\""

et cetera.

Amber
thanks. I want to choose the second option, but what should i use: "John's" or 'John\'s' - does it matter?
sami
Either will work - the point is that you have to choose a type of quote to surround your strings with, and then if you need to use whichever type you chose within the string as well, you'll have to escape it. So if you're only using 1 type of quotes within the string, it's handy to choose the other type to enclose it since you won't need to escape.
Amber
this is only possible in mysql. other database servers do not support this mixed quoting style.
longneck
@longneck: And the OP was asking in regards to MySQL.
Amber
+2  A: 

if you’re using a script to update your records use a builtin escaping function. for php that would be mysql_real_escape_string

knittl