JamieJ has asked for the wisdom of the Perl Monks concerning the following question:
When I write to a csv file from Perl using SQL Insert, I get either 0123 or """0123""", but I need "0123". Neither concatenation nor regex seem to resolve the issue.
Here's my code:
my $dbh = DBI->connect(qq{DBI:CSV:csv_eol=\n;csv_sep_char=\\,;}); $dbh->{'csv_tables'}->{'Table'} = {'file' => 'data.csv','col_names' => + ["num","id"]}; #Setup error variables $dbh->{'RaiseError'} = 1; $@ = ''; ## Attempts to change $num ##$num = '"'.$num.'"';## this causes """0123""" ##$num = "\"$num\"";## this causes """0123""" even if I additionally d +o this: ## $num=~s/"""/"/g; ##$num = " ".$num;## causes " 0123" VERY CLOSE. Try next line: ##$num=~s/ //g;## This causes 0123 ##$num = "".$num; ## causes 0123 ##$num = "'".$num."'";## causes 123 my $value = "\'$num\',\'$id\'"; my $insert = "INSERT INTO Table VALUES ($value)"; my $sth = $dbh->prepare($insert); $sth->execute(); $sth->finish(); $dbh->disconnect();
I would like to have the output of $num to end up being "0123" in the CSV, but instead I get 0123 or """0123""" or " 0123"
Any suggestions?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extra quotes doing SQL insert from Perl to CSV.
by poj (Abbot) on Oct 10, 2019 at 16:26 UTC | |
by JamieJ (Novice) on Oct 10, 2019 at 18:46 UTC | |
|
Re: Extra quotes doing SQL insert from Perl to CSV.
by Tux (Canon) on Oct 11, 2019 at 06:27 UTC | |
|
Re: Extra quotes doing SQL insert from Perl to CSV.
by choroba (Cardinal) on Oct 10, 2019 at 19:38 UTC | |
by JamieJ (Novice) on Oct 11, 2019 at 12:07 UTC |