Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Perl DBI Insert row

by barrd (Parson)
on Nov 26, 2003 at 16:35 UTC ( #310302=note: print w/ replies, xml ) Need Help??


in reply to Perl DBI Insert row

Hi Win,
As a quick guess I would say that this means that you are trying to insert a string which is bigger than the defined field size in the table Standard_population.

Try typing describe Standard_population; on the prompt and see if your data matches the criteria you set for each of the fields (i.e. are the fields data types a suitable length and type?).


Comment on Re: Perl DBI Insert row
Select or Download Code
Re: Re: Perl DBI Insert row
by Win (Novice) on Nov 26, 2003 at 16:53 UTC
    The numbers are being inserted into an int type. Is it ok to quote them?
      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.

      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.

Re: Re: Perl DBI Insert row
by mpeppler (Vicar) on Nov 26, 2003 at 17:35 UTC
    It's an MS-SQL database - describe <tablename> won't work (though sp_help <tablename> will.)

    Michael

      Thanks mpeppler,
      As is probably self evident I've never used an MS SQL variant so I'll shut up now. And again thanks for the 'heads-up', I'll avoid MS SQL related questions until actually having used one in the future :-)

      I think sp_columns<tablename> is what you may have really meant.

      Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
        sp_columns works as well, but it gives you a lot more information for each column than is really necessary (the output runs to 20 columns on my system).

        sp_help is the generic "get me information for this database object" procedure, and will give additional information, such as indexes that exist on the table, and any triggers that are defined.

        It exists in all versions of MS-SQL and Sybase dating back to the Sybase 3.x releases in the mid-late 80s... :-)

        Michael

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-10-02 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls