<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - escaping quote in Ruby]]></title>
		<link>http://railsforum.com/viewtopic.php?id=51665</link>
		<description><![CDATA[The most recent posts in escaping quote in Ruby.]]></description>
		<lastBuildDate>Wed, 19 Dec 2012 10:05:49 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[escaping quote in Ruby]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157670#p157670</link>
			<description><![CDATA[<p>Hi,</p><p>I can&#039;t seem to escape a quote in ruby. I am trying to migrate data from salesforce.com to postgres. <br />This is how my code looks like:</p><div class="codebox"><pre><code>require &#039;mailer&#039;

module ConnectionUtil
  
  def upsertRecords(objectName,queryResult,conn,salesforceOrgId)
    queryResult.each do |resultCSVRecord|
      begin
        headerList = resultCSVRecord.headers
        headerStr = headerList.map { |i| i.to_s }.join(&quot;,&quot;)
        
        if(!salesforceOrgId.nil?)
            headerStr = headerStr + &quot;,&quot; + &quot;salesforce_org_id&quot;
        end
        
        
        fieldList = resultCSVRecord.fields
        fieldStr = fieldList.map { |i| &quot;&#039;&quot; + i.to_s + &quot;&#039;&quot; }.join(&quot;,&quot;)
        
        fieldStr = fieldStr.gsub(&quot;&#039;&#039;&quot;,&quot;null&quot;)
        
        if(!salesforceOrgId.nil?)
            fieldStr = fieldStr + &quot;,&#039;&quot; + salesforceOrgId + &quot;&#039;&quot;
        end
        #conn.escape_string(fieldStr)
        #puts fieldStr
        insertScriptStr =  &quot;insert into &quot; + objectName + &quot;(&quot; + headerStr + &quot;) values (&quot; + fieldStr + &quot;)&quot;
        quote(insertScriptStr)
        #puts insertScriptStr
        
        #arr = fieldStr.split(&quot;,&quot;)
        #arr_length = arr.length
        #puts arr_length
        #prep_values = &#039;&#039;
        #for i in 1..arr_length
         #   if i != arr_length
          #    prep_values = prep_values + &quot;$&quot; + i.to_s + &quot;,&quot;    
           # else
          #    prep_values = prep_values + &quot;$&quot; + i.to_s
         #   end
        #end
        #puts prep_values
        #teststr = &quot;insert into &quot; + objectName + &quot;(&quot; + headerStr + &quot;) values (&quot; + prep_values + &quot;)&quot;
        #puts teststr
        #conn.prepare(&quot;insertscriptstr&quot;, &quot;insert into &quot; + objectName + &quot;(&quot; + headerStr + &quot;) values (&quot; + prep_values + &quot;)&quot;)  
        #conn.exec_prepared(&quot;insertscriptstr&quot;, arr)
        
        # Try inserting: If it doesn&#039;t insert then try to update
        #puts insertScriptStr
        
        insertResult = conn.exec(insertScriptStr)        
        
      rescue Exception =&gt; e  
        puts e.message  
        
        #logic to update
        updateStr = &quot;&quot;
        i = 0
          headerList.each do |hdr|
             #puts resultCSVRecord.field(i)
             if(resultCSVRecord.field(i).nil? or resultCSVRecord.field(i)==&#039;&#039;)
               updateStr = updateStr + &quot; &quot; + hdr + &quot; = NULL &quot; 
             else
               updateStr = updateStr + &quot; &quot; + hdr + &quot; = &#039;&quot; + resultCSVRecord.field(i) + &quot;&#039;&quot;
             end  
              
             i = i+1  
             if(headerList.size != i)
              updateStr = updateStr + &quot;,&quot;  
             end
          end
          
          updateScriptStr = &quot;update &quot; + objectName + &quot; set &quot; + updateStr + &quot; where Id = &#039;&quot; + resultCSVRecord.field(0) + &quot;&#039;&quot;
          updateResult = conn.exec(updateScriptStr)  
      end       
    end
    
    rescue Exception =&gt; e  
      puts e.message  
      puts e.backtrace.inspect 
    end  
  
  def quote (str)
    str.gsub(&quot;/&#039;/&quot;,&quot;\\\\&#039;&quot;)
    return str
  end

end</code></pre></div><p>The str.gsub(&quot;/&#039;/&quot;, &quot;\\\\&#039;&quot;) doesnt seem to work. I have tried many other regular expressions but none has worked for me.<br />So If I have a dynamic query insert into tablename (col1,col2,col3) values (&#039;Gaurav&#039;s&#039;,&#039;Nitin&#039;,1234) I always get the error</p><p>Syntax Error near s:</p><p>How Do I Fix this?<br />Thanks</p>]]></description>
			<author><![CDATA[dummy@example.com (gauravsingh2012)]]></author>
			<pubDate>Wed, 19 Dec 2012 10:05:49 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157670#p157670</guid>
		</item>
	</channel>
</rss>
