problem with quotes in SQL statement

Reply

Join Date: Dec 2007
Posts: 7
Reputation: ashishprem is an unknown quantity at this point 
Solved Threads: 0
ashishprem ashishprem is offline Offline
Newbie Poster

problem with quotes in SQL statement

 
0
  #1
Jan 10th, 2008
Hi
I need to insert a string in a database but the string might or might not contain a quotes('). There is no problem if there is no quotes. But in case of quotes it throws a syntax error in insert statement and that obvious also. What i am thinking is to do some formatting all the time for all the strings before inserting do the insertion. Is there anyother to achieve this.. Thanks for the suggestions.

Ashish
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,070
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 123
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

Re: problem with quotes in SQL statement

 
0
  #2
Jan 10th, 2008
if you are inserting value from VB , you can restrict the entry of single quites by restricting chr(39) in the key press event.
But if you dont want to do that replace ' by ''
i.e. single quote by 2 single quites .
Then there won't be any problem.
Share your Knowledge.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: problem with quotes in SQL statement

 
0
  #3
Jan 10th, 2008
Is there anyother to achieve this
You can remove the quotes using if statements before you insert the string into the SEQUEL statement:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim strInsert as String
  2. Dim intLength as integer
  3.  
  4. if Mid(strInsert, 1,1) = "'" then
  5. strInsert = right(strInsert, len(strInsert) -1)
  6. end if
  7.  
  8. intLength = len(strInsert)
  9.  
  10. if Mid(strInsert, intLength, 1) = "'" then
  11. strInsert = Left(strInsert, intLength -1)
  12. Endif

Hank
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 102
Reputation: SCBWV is an unknown quantity at this point 
Solved Threads: 14
SCBWV SCBWV is offline Offline
Junior Poster

Re: problem with quotes in SQL statement

 
0
  #4
Jan 10th, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: duncanj is an unknown quantity at this point 
Solved Threads: 0
duncanj duncanj is offline Offline
Newbie Poster

Re: problem with quotes in SQL statement

 
0
  #5
Jan 20th, 2008
I had a similar problem...I wanted to leave any single quotes or double quotes as they were when read in from my datasource but when trying to do this it caused problems with my SQL string in my VB code.
This is how I solved it: If my datasource involved quotes I used the string function Replace to make them two single quotes in my SQL statement ie:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. strQuery = "update files set files." & sField & " = '" & Replace(strOut, "'", "''") & "' where whatever=" & adoRS("whatever")
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 300
Reputation: jireh is an unknown quantity at this point 
Solved Threads: 37
jireh's Avatar
jireh jireh is offline Offline
Posting Whiz

Re: problem with quotes in SQL statement

 
0
  #6
Jan 21st, 2008
I agree with you duncanj that problem is very common when it comes to queries, most programmer used that function (Replace())
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC