Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re (3): Perl DBI Insert row

by VSarkiss (Monsignor)
on Nov 26, 2003 at 17:32 UTC ( #310326=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Perl DBI Insert row
in thread Perl DBI Insert row

No, it isn't. The error you're getting is that the string data (for example, 7000) will not fit into the intended column. If you want to insert the number 7000, omit the quotes.

You can forget about all this with DBI by using placeholders. Your statement would be something like:

my $sthC = $dbh->prepare("insert TABLE (col1, col2, ...) values(?, ?, ?)") or die "Couldn't prepare: $DBI::errstr"; $sthC->execute('value1', 'value2', ...) or die "Couldn't execute: $DBI::errstr";
The nice thing is that Perl offers you many ways to tidy that up. For example, if you have an array containing the values to be inserted, you can pass that to execute. Also, you can format the SQL string using all the power of the language. My personal preference is usually something like this (this isn't working code, just an example):
my @values = qw(...); my $sql = q{ insert table ( col1 , col2 , ... ) values }; $sql .= '(' . join(',', ('?') x scalar(@values)) . ')'; my $sth = $dbh->prepare($sql) or die "Couldn't prepare $sql: $DBI::errstr"; $sth->execute(@values) or die "Couldn't execute $sql: $DBI::errstr";


Expanded example.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://310326]
[Corion]: Oh, poor Trump can't get a break. Even the Vatican only sends the second in command to greet him...
[shmem]: Discipulus: readdir has the same semantics as readline (a.k.a <>). Have another coffee ☕ ;-)
[Discipulus]: ah! just with my old system perl (5.8.8) was needed
[Corion]: Has anybody toyed around with Mastodon? There is a Perl module for it, but I'm not sure that it's a yak worth chasing
[Corion]: On the upside, maybe running that for my family would be interesting, or at least for myself
[Corion]: I'm not fond of using a server hosted by other people, even if they claim to be GNU ;)
[Discipulus]: gee a social network with the family.. is this a dread? ;=)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-05-26 07:35 GMT
Find Nodes?
    Voting Booth?