Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting single quotes into database

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting "missing comma" SQL error while inserting a String that contains single quotes in it. for example, the below insert statement code:



now, the above code will throw run time sql error because the insert statement has single quotes in manager's word as shown below:

insert into emp (detail) values ('its manager's duty.')

i guess this will be a very common problem. so, is there any standard way or in-built method to resolve the issue. please help. thank you.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try 'The manager''s duty'
Try 'The manager\' duty'

Note first example is single quote twice, not double quote.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or better still, use a PreparedStatement:

That way you don't need to worry about quotes. Or date formatting, or any other SQL formatting issues. The JDBC driver takes care of all of the escaping for you.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you campbell and paul.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic