Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Error while inserting null values with DBI

by mpeppler (Vicar)
on Jan 24, 2008 at 10:02 UTC ( #663977=note: print w/replies, xml ) Need Help??


in reply to Error while inserting null values with DBI

A couple of solutions.

1. Use placeholders:

my $sth = $dbh->prepare("insert test(id, name) values(?, ?)"); $sth->execute($id, $name);
If $id is undef then a correct NULL value will be passed to the data server (which from the error message would appear to be Sybase or MS-SQL).

An alternative solution:

$id = 'NULL' unless defined($id); $dbh->do("insert test(id, name) values($id, \"$name\")");
The latter solution is much inferior to the placeholder solution, and is incomplete and bug-prone (i.e. if $name is NULL instead of $id you need to handle this differently, not to mention quoting issues with strings...)

Michael

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://663977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2020-10-30 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (279 votes). Check out past polls.

    Notices?