ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 7th, 2005, 08:55 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
I have a tricky single quote problem

I have a form where people can enter multiple names (name1, name2, name3, etc). I then turn their entries into an array, loop through it, and insert it into a table. It works fine UNLESS the name has an apostrophe in it (Jim O'Grady, for example).

First, here's the query:
Code:
<CFLOOP INDEX="i" FROM="1" TO="#loop_cnt#">

<CFQUERY NAME="upload" DATASOURCE="#datasource#">
INSERT into self_report_multi_file
(report_group_key,
report_nbr,
report_date,
case_nbr,
loan_officer,
loan_nbr,
self_sw)
values ('#repkey#', #i#, '#DateFormat(NOW(), "mm/dd/yyyy")#', '#Trim(aCaseNumbers[i][1])#', '#Trim(aCaseNumbers[i][2])#', '#Trim(aCaseNumbers[i][3])#', 'U')
</CFQUERY>


aCaseNumbers[i][1] and aCaseNumbers[i][3] are not a worry, because they're numbers. aCaseNumbers[i][2] is the name, and if that has the ' in it, the query bombs. Here's the error I get:

Sybase Error Code = 105

Unclosed quote before the character string ')'.

SQL = "INSERT into self_report_multi_file (report_group_key, report_nbr, report_date, case_nbr, loan_officer, loan_nbr, self_sw) values ('040705092830', 3, '04/07/2005', '111-1111', 'Sam O'Jones', '55555', 'U')"

It's easy to see what the problem is...when the query hits that ' in "O'Jones" it reads it as a single-quote in the query.

I've tried using PreserveSingleQuotes a bunch of places, but everything I try doesn't seem to work. Right now I have it looping through and replacing any ' in the name with a space, but that's not exactly the best solution. Any ideas? Is it possible to use the HTML number for the single quote (&#39)?

Thanks.

Reply With Quote
  #2  
Old April 7th, 2005, 08:58 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
Hah, nevermind. All I had to do was change '#Trim(aCaseNumbers[i][2])#' in the query to "#Trim(aCaseNumbers[i][2])#" ...sometimes the simplest solutions are the last ones you think of

Reply With Quote
  #3  
Old April 7th, 2005, 09:18 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
Bah. Now if, for some reason, the name has a " in it, the query bombs...Normally names don't have " in them, but it's still something I'd like to take care of...Is there a way to deal with this sort of thing? Because something like <CFSET aCaseNumbers[i][2] = #Replace(aCaseNumbers[i][2], """, " ")#> doesn't work because of the quotes... I tried Evaluate, but that didn't work (or my syntax was wrong).

Reply With Quote
  #4  
Old April 7th, 2005, 09:43 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
I know I'm talking to myself here, but I thought I'd add some closure. I don't know if the following was the best way to take care of this, but it works!

<cfset double_quote = '"'>
<cfset single_quote = "'">

<CFLOOP INDEX="i" FROM="1" TO="#loop_cnt#">
<CFIF aCaseNumbers[i][2] CONTAINS "#double_quote#">
<CFSET aCaseNumbers[i][2] = #Replace(aCaseNumbers[i][2], "#double_quote#", "#single_quote#", 'ALL')#>
</CFIF>
</CFLOOP>

Reply With Quote
  #5  
Old April 7th, 2005, 12:20 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
I think you could just keep the single quotes and wrap the data in the preserveSingleQuotes() function. I also think you could use <cfqueryparam> which I believe handles any character escaping issues.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > I have a tricky single quote problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway