Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to escape single quotes inside fields but not the ones aroundfields?

Reply
Thread Tools

How to escape single quotes inside fields but not the ones aroundfields?

 
 
Henry J.
Guest
Posts: n/a
 
      10-06-2008
Need to escape single quotes ( i.e., ' -> '' ) in a data file before
sending to DB as part of insert SQLs.

Example 1):

it's mine, it's yours, 12, 42, 2008/10/06 => it''s mine,
it''s yours, 12, 42, 2008/10/06

Example 2):

'it's mine', 'it's yours', 12, 42, '2008/10/06' => 'it''s
mine', 'it''s yours', 12, 42, '2008/10/06'

The tricky part is that the data file may or may not have the string
fields wrapped in single quotes. In Example 1), it is not and single
quotes around fields will be added by another script before sending to
DB, in Example 2), its fields are already enclosed in single quotes
and will be sent to DB as is.

Does anybody have handy perl one-liner or script that tackles this?
Thanks!

 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-06-2008

Henry J. wrote:
> Need to escape single quotes ( i.e., ' -> '' ) in a data file before
> sending to DB as part of insert SQLs.
>
> Example 1):
>
> it's mine, it's yours, 12, 42, 2008/10/06 => it''s mine,
> it''s yours, 12, 42, 2008/10/06
>
> Example 2):
>
> 'it's mine', 'it's yours', 12, 42, '2008/10/06' => 'it''s
> mine', 'it''s yours', 12, 42, '2008/10/06'
>
> The tricky part is that the data file may or may not have the string
> fields wrapped in single quotes. In Example 1), it is not and single
> quotes around fields will be added by another script before sending to
> DB, in Example 2), its fields are already enclosed in single quotes
> and will be sent to DB as is.
>
> Does anybody have handy perl one-liner or script that tackles this?
> Thanks!
>


$dbh->prepare("SELECT foo, bar FROM table WHERE baz=?")->execute($baz);



--
RGB
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-06-2008
On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <> wrote:

>Need to escape single quotes ( i.e., ' -> '' ) in a data file before
>sending to DB as part of insert SQLs.
>
> Example 1):
>
> it's mine, it's yours, 12, 42, 2008/10/06 => it''s mine,
>it''s yours, 12, 42, 2008/10/06
>
> Example 2):
>
> 'it's mine', 'it's yours', 12, 42, '2008/10/06' => 'it''s
>mine', 'it''s yours', 12, 42, '2008/10/06'
>
>The tricky part is that the data file may or may not have the string
>fields wrapped in single quotes. In Example 1), it is not and single
>quotes around fields will be added by another script before sending to
>DB, in Example 2), its fields are already enclosed in single quotes
>and will be sent to DB as is.
>
>Does anybody have handy perl one-liner or script that tackles this?
>Thanks!



I don't think this can be a one liner, maybe.
In esscence this does it all:

sln

#############
# Delim Regex
#############

use strict;
use warnings;

my $str = "
it's mine, it's yours, 12, 42, 2008/10/06, 'it's mine', 'it's yours', 12, 42, '2008/10/06',
it's wants it's gets ";


while ($str =~ /([^,\n]+)/g)
{
my $val = $1;
$val =~ s/^[\s'"]+//; $val =~ s/[\s'"]+$//;
$val =~ s/'/''/g;
$val = "'$val'";
print "val = $val\n";
}


__END__

output:

val_a = 'it''s mine'
val_a = 'it''s yours'
val_a = '12'
val_a = '42'
val_a = '2008/10/06'
val_a = 'it''s mine'
val_a = 'it''s yours'
val_a = '12'
val_a = '42'
val_a = '2008/10/06'
val_a = 'it''s wants it''s gets'



 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-06-2008
On Mon, 06 Oct 2008 18:25:07 GMT, wrote:

>On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <> wrote:
>
>>Need to escape single quotes ( i.e., ' -> '' ) in a data file before
>>sending to DB as part of insert SQLs.
>>
>> Example 1):
>>
>> it's mine, it's yours, 12, 42, 2008/10/06 => it''s mine,
>>it''s yours, 12, 42, 2008/10/06
>>
>> Example 2):
>>
>> 'it's mine', 'it's yours', 12, 42, '2008/10/06' => 'it''s
>>mine', 'it''s yours', 12, 42, '2008/10/06'
>>
>>The tricky part is that the data file may or may not have the string
>>fields wrapped in single quotes. In Example 1), it is not and single
>>quotes around fields will be added by another script before sending to
>>DB, in Example 2), its fields are already enclosed in single quotes
>>and will be sent to DB as is.
>>
>>Does anybody have handy perl one-liner or script that tackles this?
>>Thanks!

>
>
>I don't think this can be a one liner, maybe.
>In esscence this does it all:
>
>sln
>
>#############
># Delim Regex
>#############
>
>use strict;
>use warnings;
>
>my $str = "
>it's mine, it's yours, 12, 42, 2008/10/06, 'it's mine', 'it's yours', 12, 42, '2008/10/06',
>it's wants it's gets ";
>
>
>while ($str =~ /([^,\n]+)/g)
>{
> my $val = $1;
> $val =~ s/^[\s'"]+//; $val =~ s/[\s'"]+$//;


# just for grins, reconstitute partial fixes.
# there goes that one-liner possibility

$val =~ s/'+/'/g;
#

> $val =~ s/'/''/g;
> $val = "'$val'";
> print "val = $val\n";
>}
>
>
>__END__
>
>output:
>
>val_a = 'it''s mine'
>val_a = 'it''s yours'
>val_a = '12'
>val_a = '42'
>val_a = '2008/10/06'
>val_a = 'it''s mine'
>val_a = 'it''s yours'
>val_a = '12'
>val_a = '42'
>val_a = '2008/10/06'
>val_a = 'it''s wants it''s gets'
>
>


 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      10-06-2008
>>>>> "HJ" == Henry J <> writes:

HJ> Need to escape single quotes ( i.e., ' -> '' ) in a data file
HJ> before sending to DB as part of insert SQLs.

Use placeholders in your SQL statement, and let the database driver
handle escaping as necessary. perldoc DBI

Charlton



--
Charlton Wilbur

 
Reply With Quote
 
Henry J.
Guest
Posts: n/a
 
      10-06-2008
On Oct 6, 2:25*pm, s...@netherlands.com wrote:
> On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:
> >Need to escape single quotes ( i.e., ' -> '' ) in a data file before
> >sending to DB as part of insert SQLs.

>
> > * * * Example 1):

>
> > * * * * * it's mine, it's yours, 12, 42, 2008/10/06 * => it''s mine,
> >it''s yours, 12, 42, 2008/10/06

>
> > * * * Example 2):

>
> > * * * * * 'it's mine', 'it's yours', 12, 42, '2008/10/06' *=> 'it''s
> >mine', 'it''s yours', 12, 42, '2008/10/06'

>
> >The tricky part is that the data file may or may not have the string
> >fields wrapped in single quotes. *In Example 1), it is not and single
> >quotes around fields will be added by another script before sending to
> >DB, in Example 2), its fields are already enclosed in single quotes
> >and will be sent to DB as is.

>
> >Does anybody have handy perl one-liner or script that tackles this?
> >Thanks!

>
> I don't think this can be a one liner, maybe.
> In esscence this does it all:
>
> sln
>
> #############
> # Delim Regex
> #############
>
> use strict;
> use warnings;
>
> my $str = "
> it's mine, it's yours, 12, 42, 2008/10/06, 'it's mine', 'it's yours', 12,42, '2008/10/06',
> it's wants it's gets ";
>
> while ($str =~ /([^,\n]+)/g)
> {
> * * * * my $val = $1;
> * * * * $val =~ s/^[\s'"]+//; $val =~ s/[\s'"]+$//;
> * * * * $val =~ s/'/''/g;
> * * * * $val = "'$val'";
> * * * * print "val = $val\n";
>
> }
>
> __END__
>
> output:
>
> val_a = 'it''s mine'
> val_a = 'it''s yours'
> val_a = '12'
> val_a = '42'
> val_a = '2008/10/06'
> val_a = 'it''s mine'
> val_a = 'it''s yours'
> val_a = '12'
> val_a = '42'
> val_a = '2008/10/06'
> val_a = 'it''s wants it''s gets'- Hide quoted text -
>
> - Show quoted text -


Thanks a lot! I'm able to adapt it a bit to solve my problem (e.g., I
cannot add or remove enclosing single quotes).
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-06-2008
Henry J. schreef:

> Need to escape single quotes ( i.e., ' -> '' ) in a data file before
> sending to DB as part of insert SQLs.


No, you don't.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-06-2008
On Mon, 6 Oct 2008 11:55:02 -0700 (PDT), "Henry J." <> wrote:

>On Oct 6, 2:25*pm, s...@netherlands.com wrote:
>> On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:
>> >Need to escape single quotes ( i.e., ' -> '' ) in a data file before
>> >sending to DB as part of insert SQLs.

>>
>> > * * * Example 1):

>>
>> > * * * * * it's mine, it's yours, 12, 42, 2008/10/06 * => it''s mine,
>> >it''s yours, 12, 42, 2008/10/06

>>
>> > * * * Example 2):

>>
>> > * * * * * 'it's mine', 'it's yours', 12, 42, '2008/10/06' *=> 'it''s
>> >mine', 'it''s yours', 12, 42, '2008/10/06'

>>
>> >The tricky part is that the data file may or may not have the string
>> >fields wrapped in single quotes. *In Example 1), it is not and single
>> >quotes around fields will be added by another script before sending to
>> >DB, in Example 2), its fields are already enclosed in single quotes
>> >and will be sent to DB as is.

>>
>> >Does anybody have handy perl one-liner or script that tackles this?
>> >Thanks!

>>
>> I don't think this can be a one liner, maybe.
>> In esscence this does it all:
>>
>> sln
>>
>> #############
>> # Delim Regex
>> #############
>>
>> use strict;
>> use warnings;
>>
>> my $str = "
>> it's mine, it's yours, 12, 42, 2008/10/06, 'it's mine', 'it's yours', 12, 42, '2008/10/06',
>> it's wants it's gets ";
>>
>> while ($str =~ /([^,\n]+)/g)
>> {
>> * * * * my $val = $1;
>> * * * * $val =~ s/^[\s'"]+//; $val =~ s/[\s'"]+$//;
>> * * * * $val =~ s/'/''/g;
>> * * * * $val = "'$val'";
>> * * * * print "val = $val\n";
>>
>> }
>>
>> __END__
>>
>> output:
>>
>> val_a = 'it''s mine'
>> val_a = 'it''s yours'
>> val_a = '12'
>> val_a = '42'
>> val_a = '2008/10/06'
>> val_a = 'it''s mine'
>> val_a = 'it''s yours'
>> val_a = '12'
>> val_a = '42'
>> val_a = '2008/10/06'
>> val_a = 'it''s wants it''s gets'- Hide quoted text -
>>
>> - Show quoted text -

>
>Thanks a lot! I'm able to adapt it a bit to solve my problem (e.g., I
>cannot add or remove enclosing single quotes).


Without at least removeing enclosing quotes, the inner quote would be hard to
substitute. Not impossible, but harder. Its easy to test if there is enclosing
quote (then add it back later), but it still has to be removed before doing the
inner quote substitution.

Either way it can be done. How did you do it?

Thanks!

sln

 
Reply With Quote
 
Henry J.
Guest
Posts: n/a
 
      10-06-2008
On Oct 6, 3:48*pm, s...@netherlands.com wrote:
> On Mon, 6 Oct 2008 11:55:02 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:
> >On Oct 6, 2:25*pm, s...@netherlands.com wrote:
> >> On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:
> >> >Need to escape single quotes ( i.e., ' -> '' ) in a data file before
> >> >sending to DB as part of insert SQLs.

>
> >> > * * * Example 1):

>
> >> > * * * * * it's mine, it's yours, 12, 42, 2008/10/06 * => it''s mine,
> >> >it''s yours, 12, 42, 2008/10/06

>
> >> > * * * Example 2):

>
> >> > * * * * * 'it's mine', 'it's yours', 12, 42, '2008/10/06' *=> 'it''s
> >> >mine', 'it''s yours', 12, 42, '2008/10/06'

>
> >> >The tricky part is that the data file may or may not have the string
> >> >fields wrapped in single quotes. *In Example 1), it is not and single
> >> >quotes around fields will be added by another script before sending to
> >> >DB, in Example 2), its fields are already enclosed in single quotes
> >> >and will be sent to DB as is.

>
> >> >Does anybody have handy perl one-liner or script that tackles this?
> >> >Thanks!

>
> >> I don't think this can be a one liner, maybe.
> >> In esscence this does it all:

>
> >> sln

>
> >> #############
> >> # Delim Regex
> >> #############

>
> >> use strict;
> >> use warnings;

>
> >> my $str = "
> >> it's mine, it's yours, 12, 42, 2008/10/06, 'it's mine', 'it's yours', 12, 42, '2008/10/06',
> >> it's wants it's gets ";

>
> >> while ($str =~ /([^,\n]+)/g)
> >> {
> >> * * * * my $val = $1;
> >> * * * * $val =~ s/^[\s'"]+//; $val =~ s/[\s'"]+$//;
> >> * * * * $val =~ s/'/''/g;
> >> * * * * $val = "'$val'";
> >> * * * * print "val = $val\n";

>
> >> }

>
> >> __END__

>
> >> output:

>
> >> val_a = 'it''s mine'
> >> val_a = 'it''s yours'
> >> val_a = '12'
> >> val_a = '42'
> >> val_a = '2008/10/06'
> >> val_a = 'it''s mine'
> >> val_a = 'it''s yours'
> >> val_a = '12'
> >> val_a = '42'
> >> val_a = '2008/10/06'
> >> val_a = 'it''s wants it''s gets'- Hide quoted text -

>
> >> - Show quoted text -

>
> >Thanks a lot! *I'm able to adapt it a bit to solve my problem (e.g., I
> >cannot add or remove enclosing single quotes).

>
> Without at least removeing enclosing quotes, the inner quote would be hard to
> substitute. Not impossible, but harder. Its easy to test if there is enclosing
> quote (then add it back later), but it still has to be removed before doing the
> inner quote substitution.
>
> Either way it can be done. How did you do it?
>
> Thanks!
>
> sln- Hide quoted text -
>
> - Show quoted text -


I replace it with a string and then replace it back.

BTW, i cannot use the placeholders to handle strings because this is a
generic function in a perl lib that works like bcp in sybase (i.e.,
loading a given data file into any given table in DB).
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-06-2008
On Mon, 6 Oct 2008 12:58:14 -0700 (PDT), "Henry J." <> wrote:

>On Oct 6, 3:48*pm, s...@netherlands.com wrote:
>> On Mon, 6 Oct 2008 11:55:02 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:
>> >On Oct 6, 2:25*pm, s...@netherlands.com wrote:
>> >> On Mon, 6 Oct 2008 08:16:38 -0700 (PDT), "Henry J." <tank209...@yahoo.com> wrote:

>
>I replace it with a string and then replace it back.
>
>BTW, i cannot use the placeholders to handle strings because this is a
>generic function in a perl lib that works like bcp in sybase (i.e.,
>loading a given data file into any given table in DB).


Ok, workarounds are acceptable.. good luck.
Btw, I used to do alot of ADO back in the day. In need of a job if you know of any.

sln

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Escape character for single quotes. Is it really required in String? Mausam Java 4 02-07-2012 07:11 PM
Backticks with single quote inside single quotes john.d.russell@gmail.com Perl Misc 10 04-17-2006 01:31 AM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
escape single and double quotes Leif B. Kristensen Python 7 03-24-2005 11:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57