[ic] SQL write to a table

Mike Heins mike at perusion.com
Mon Dec 6 08:53:01 EST 2004


Quoting Daniel Davenport (ddavenport at newagedigital.com):
> (pardon any icky formatting. Work makes me use groupwise.  :P )
> >::logDebug(qq{\nBEFORE SQL });
> >
> >   # store the license data in the database under record number
> >   my $dbh = $Sql{state_licenses}
> >             or return HTML "Database not shared.";
> >
> >   my $q = 'INSERT INTO state_licenses (order_number, state,
> license)'
> >          . ' VALUES ("0000110", "WA", "WSDL7777")';
> >
> >   my $sth = $dbh->prepare( $q )
> >             or return HTML "Couldn't open database";
> >
> >   $rc = $sth->execute()
> >             or return HTML "Execute sql failed";
> >
> >   $sth->finish();
> >   $dbh->disconnect();
> >
> >::logDebug(qq{\nAFTER SQL });
> 
> try this instead.  :)
>  
> $sql = "INSERT INTO state_licenses (order_number, state, license)" 
>      . "VALUES ('0000110', 'WA', 'WSDL7777')";
> $Tag->query({sql => $sql, table => "state_licenses" }) or return HTML
> "Insert failed."; 
>  
> BTW, if you intend to use cgi variables and such in the insert instead
> of the values you have in there right now, you'll want to (read: you
> better) filter the results using either
>  
> $some_value =~ s/([\\'])/\1\1/g;
>  
> or
>  
> $value = $Tag->filter({op => 'sql'}, $value);
>  
> the former is preferable for mysql databases, as it also escapes
> backslashes (which mysql can use for quoting, unlike most other
> dbms's).

I still think easiest of all is:

[perl tables=state_license]
	my $db = $Db{state_license};
	my %record = (
	    order_number => '0000110',
	    state        => 'WA',
	    license      => 'WSDL7777',
	);

	my $new_key = $db->set_slice(undef, \%record);

[/perl]

you don't need to worry about formatting the SQL, filtering the values,
or anything.

i understand that some people have a certain level of familiarity with
SQL and DBI, but for simple single-record setting there is no need to
go through all that.

-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.765.647.1295  tollfree 800-949-1889 <mike at perusion.com>

p.s. sorry for lower case, injured hand

"Even if you're on the right track, you'll get run over if you just
sit there." -- Will Rogers


More information about the interchange-users mailing list