Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Perl DBI Insert row

by Win (Novice)
on Nov 26, 2003 at 16:53 UTC ( #310310=note: print w/ replies, xml ) Need Help??


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

The numbers are being inserted into an int type. Is it ok to quote them?


Comment on Re: Re: Perl DBI Insert row
Re: Re: Re: Perl DBI Insert row
by barrd (Parson) on Nov 26, 2003 at 17:02 UTC
    Well, from the data you supplied in the OP as long as each of the fields that will be taking numerical only data is set to int(4) (or greater) you 'should' be OK.

    As for quoting, in your example yes if using '0123' as an example*, but you probably want to be looking at placeholders for good coding practice. Do a Super Search on placeholders for more detailed info.

    *Update: As VSarkiss pointed out I was talking utter rubbish ;) ... but the placeholders part is still true.

Re (3): Perl DBI Insert row
by VSarkiss (Monsignor) on Nov 26, 2003 at 17:32 UTC

    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";

    HTH

    UPDATE
    Expanded example.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-02 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls