• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting strings with quotes

 
Carey Brown
Bartender
Pie
Posts: 1635
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an escape sequence that you can use to insert a string that contains a single quote? e.g.; "Carey's data". I tried a backslash but it didn't work, the backslash actually ended up in the database.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is to use a PreparedStatement instead of Statement. PreparedStatements automatically escape these troublesome characters. The other method is to escape single quotes with another single quote:
"insert into ... values ( "Sam", "O''Reilly" )
this will insert "O'Reilly" into the database.
Jamie
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie - is that two single quotes between the O and R? Or a double quote?
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you've probably figured this out by now, but you are right, it should be:
"insert into ... values ( 'Sam', 'O''Reilly' )
this will insert O'Reilly into the database.
Jamie
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O'Really? ;-)
It'll mess up your HTML & Javascript too if you're not careful.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic