List:General Discussion« Previous MessageNext Message »
From:zzapper Date:January 14 2005 7:22pm
Subject:Re: Perl Script to transform a MySql insert into an update
View as plain text  
On Fri, 14 Jan 2005 18:42:41 +0000,  wrote:

>Hi,
>Does anyone have a Perl Script (or other) to transform a MySql insert into an update
> (or
>vice-versa)???

Just wrote a QAD (Quick and dirty) script see below, has no error handling

#!perl
# sedit.pl
# description : Transform a mysql insert into an update statement
# zzapper david@stripped
# NB does not handle any commas in strings etc

$insert_str=q|(fred,joe) values ('$fred','$joe')|;

$_=$insert_str;
s#^\s*\(([^)]+)\)##;
$set_str=$1;
s#values\s+\(([^)]+)\)##;
$values_str=$1;
@set_array=split(/,/,$set_str);
@values_array=split(/,/,$values_str);

$update_str.=qq|set |;
for $setv (@set_array)
{
$valv=$values_array[$cnt++];
$update_str.=qq|$setv=$valv,|;
}
$update_str=~s/,$//;
print "\ninsert :: $insert_str\n";
print "\nupdate :: $update_str\n";


zzapper (vim, cygwin, wiki & zsh)
--

vim -c ":%s%s*%CyrnfrTfcbafbeROenzSZbbyranne%|:%s)[R-T]) )Ig|:norm G1VGg?"

http://www.vim.org/tips/tip.php?tip_id=305  Best of Vim Tips

Thread
Perl Script to transform a MySql insert into an updatezzapper14 Jan
  • Re: Perl Script to transform a MySql insert into an updatezzapper14 Jan