If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Creating UPDATE statement when values contain single quote or double quote

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-27-10, 14:46
mustangarcher mustangarcher is offline
Registered User
 
Join Date: Oct 2010
Posts: 2
Creating UPDATE statement when values contain single quote or double quote

Hello. I am trying to figure out the correct syntax for an SQL statement to UPDATE a table with a value that contains a quote or apostrophe. I am pasting a snippet below. I have included in my code substitute reg expressions to escape quotes and apostrophes. I get a software error whether or not I include the escapes. The error: Syntax error (missing operator) in query expression
Thank you for looking.

$dbh = DBI->connect('dbi:ODBC:driver=microsoft access driver (*.mdb);dbq=\\\\cessna3\\dept425\\YellowFlag\\atdf lags.mdb')
or die "Can't open the database." ;

$updatecondition =~ s/(['"])/\\$1/g;
$updateskillcoach =~ s/(['"])/\\$1/g;
$updatenotes =~ s/(['"])/\\$1/g;

$SQL = qq(UPDATE Flags SET Condition = '$updatecondition',SkillCoach = '$updateskillcoach',Notes = '$updatenotes' WHERE ID = $recordid);

$dbh->do($SQL) or die "$DBI::errstr";


$dbh->disconnect;
Reply With Quote
  #2 (permalink)  
Old 10-28-10, 02:33
spacebar spacebar is offline
Registered User
 
Join Date: Feb 2006
Posts: 73
Show what $SQL evaluates to right before you execute it:
Code:
$SQL = qq(UPDATE Flags SET Condition = '$updatecondition',SkillCoach =  '$updateskillcoach',Notes = '$updatenotes' WHERE ID = $recordid);
Reply With Quote
  #3 (permalink)  
Old 10-28-10, 08:24
mustangarcher mustangarcher is offline
Registered User
 
Join Date: Oct 2010
Posts: 2
Appied bonnets

Quote:
Originally Posted by spacebar View Post
Show what $SQL evaluates to right before you execute it:
Code:
$SQL = qq(UPDATE Flags SET Condition = '$updatecondition',SkillCoach =  '$updateskillcoach',Notes = '$updatenotes' WHERE ID = $recordid);
I don't know how to do that. I think I got it working anyway. I ended up escaping the apostrophe with another apostrophe and I was incorrect in thinking that a double quote needs escaped. So it is updating correctly for now. Thanks for looking.
Reply With Quote
  #4 (permalink)  
Old 11-15-10, 10:13
bchanan bchanan is offline
Registered User
 
Join Date: Dec 2009
Posts: 27
better idea:

check cpan using DBI for quote method:
DBI - search.cpan.org

Chanan
Reply With Quote
  #5 (permalink)  
Old 11-26-10, 03:09
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
quote works, but you can let DBI do all the work.

Code:
$SQL = qq(UPDATE Flags SET Condition = ?,SkillCoach = ?,Notes = ? WHERE ID = ?);

$dbh->do($SQL, {}, $updatecondition, $updateskillcoach, $updatenotes, $recordid) or die "$DBI::errstr";
Read about placeholders and bind values here. There's also a section describing how the do method works.

To print the SQL you're trying to pass, just:

Code:
print $SQL."\n";
Reply With Quote
Reply

Tags
apostrophe, dbi, escape, perl, update

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On