SunQuest
           Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

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:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old November 21st, 2000, 04:48 AM
syin syin is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 13 syin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

I have an array @body, which is actually the body of an email. In the contents of the email, there are some "", so when I insert @body into mySQL, all the "" disappear because they are used for escape characters such as n. How can I correct this problem? (double quotes work fine though..)

The way I insert it is like this:
$dbh->do("INSERT INTO tbl VALUES ('@body')");
where the field is of type LONGTEXT.

I was trying something like
@body =~ s///;
in an attempt to replace all single backslashes to double backslashes, but apparently the above has incorrect syntax.

Thanks for the help in advance!

Reply With Quote
  #2  
Old November 21st, 2000, 05:53 PM
amead amead is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 5 amead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by syin:
I have an array @body, which is actually the body of an email. In the contents of the email, there are some "", so when I insert @body into mySQL, all the "" disappear because they are used for escape characters such as n.[/quote]

There is a DBI call to fix this. You have other sequences to worry about (like single quotes). The function is $dbh->quote($string);

I don't think you can use it to do entire arrays (try!)... if not, you would do this:

foreach $line (@body) { $dbh->quote($line); }

This function does the same sort of substitution you tried... you just need to double your slashes:

foreach (@body) { s///; }

DISCLAIMER: This is all from memory and untested. Good luck.

Reply With Quote
  #3  
Old November 21st, 2000, 08:17 PM
syin syin is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 13 syin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
amead:

Thank you very much for your help. I used

foreach (@body) { s///; }

and it worked. However, when I have a path like: dirsubdirfile, the above only change it to dirsubdirfile. Why is that? I thought it would find all instances of and replace it with , so I was expecting dirsubdirfile. What did I do wrong?

Also, I gave the quote() a try. First of all, I did the experiement and it did not take an array as an arguement. It went thru when I try your other suggested method (foreach $line....), but somehow the string coming out is exactly the same as before. I am wondering if quote() put the result somewhere else instead of modifiy the variable I pass to it? I want the modified string to be in the @body array...

Anyway, thanks again for your help!

Reply With Quote
  #4  
Old November 21st, 2000, 09:45 PM
amead amead is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 5 amead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by syin:
amead:

Thank you very much for your help. I used

foreach (@body) { s///; }

and it worked. However, when I have a path like: dirsubdirfile, the above only change it to dirsubdirfile.
[/quote]

I forgot that you need to append a 'g' to the sub:

foreach (@body) { s///g; }


<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Also, I gave the quote() a try. [...] when I try your other suggested method (foreach $line....), but somehow the string coming out is exactly the same as before. I am wondering if quote() put the result somewhere else instead of modifiy the variable I pass to it?[/quote]

Hmm. Try:

foreach $item (@body) {
$item = $dbh->quote($item);
}

Of course, only lines with slashes, single apostrophes, newlines, etc. will be affected.

-Alan

Reply With Quote
  #5  
Old November 21st, 2000, 10:13 PM
syin syin is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 13 syin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks again. I used the /g at the end of the pattern match and it worked. I didn't use quote because the result is not too HTML-friendly.


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Taking care of backslashes (related to mySQL)


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 2 hosted by Hostway