Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl DBI Insert row

by Win (Novice)
on Nov 26, 2003 at 16:08 UTC ( [id://310293]=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Can anyone please explain why the following bit of code:

my $sthC = $dbh->prepare("INSERT into Standard_population (Standar +disation_type, Male_00_04, Female_00_04, Male_05_09, Female_05_09, Ma +le_10_14, Female_10_14, Male_15_19, Female_15_19, Male_20_24, Female_ +20_24, Male_25_29, Female_25_29, Male_30_34, Female_30_34, Male_35_39 +, Female_35_39, Male_40_44, Female_40_44, Male_45_49, Female_45_49, M +ale_50_54, Female_50_54, Male_55_59, Female_55_59, Male_60_64, Female +_60_64, Male_65_69, Female_65_69, Male_70_74, Female_70_74, Male_75_7 +9, Female_75_79, Male_80_84, Female_80_84, Male_85_plus, Female_85_pl +us) VALUES ('EuropeanStandard','8000','8000','7000','7000','7000','70 +00','7000','7000','7000','7000','7000','7000','7000','7000','7000','7 +000','7000','7000','7000','7000','7000','7000','6000','6000','5000',' +5000','4000','4000','3000','3000','2000','2000','1000','1000','1000', +'1000');") or die "Couldn't prepare query: ".$dbh->errstr; $sthC->execute() or die "Couldn't execute query: ".$sthC->errstr;

Gives the following error messages:

DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver][SQL +Server]Str ing or binary data would be truncated. (SQL-22001) [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been +terminated . (SQL-01000)(DBD: st_execute/SQLExecute err=-1) at Creating_table_H.p +l line 225 . Couldn't execute query: [Microsoft][ODBC SQL Server Driver][SQL Server +]String or binary data would be truncated. (SQL-22001) [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been +terminated . (SQL-01000)(DBD: st_execute/SQLExecute err=-1) at Creating_table_H.p +l line 225

Replies are listed 'Best First'.
Re: Perl DBI Insert row
by barrd (Canon) on Nov 26, 2003 at 16:35 UTC
    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?).

      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"
      The numbers are being inserted into an int type. Is it ok to quote them?

        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.

        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: Perl DBI Insert row
by Roger (Parson) on Nov 27, 2003 at 04:27 UTC
    Too messy, too messy. Why don't you build your SQL insert dynamically?
    # set up some sample data, assume $#columns == $#values my @columns = qw / Standardisation_type Male_00_04 Female_85_plus /; my @values = qw / EuropeanStandard 7000 7000 /; # Dynamically build the SQL insert with place holders my $sql = "INSERT INTO Standard_population(" . join(',',@columns) . ") " . "VALUES (" . join(',', map {'?'} @columns) . ")"; my $sth = $dbh->prepare($sql) or die "Couldn't prepare query"; $sth->execute(@values);
    The code generates the following SQL with placeholders:
    INSERT INTO Standard_population(Standardisation_type,Male_00_04,Female +_85_plus) VALUES (?,?,?)
Re: Perl DBI Insert row
by Itatsumaki (Friar) on Nov 26, 2003 at 18:07 UTC

    One problem that jumps out at me is the ; that you added to the end of your SQL statement. For clarity's sake, let's break this down a bit by separating the SQL from the $sth, and fixing that extra semi-colon. If I were you, I would also unquote the numbers if they are going into a numeric field, and also use place-holders.

    my $sql = " INSERT into Standard_population ( Standardisation_type, Male_00_04, Female_00_04, Male_05_09, Female_05_09, Male_10_14, Female_10_14, Male_15_19, Female_15_19, Male_20_24, Female_20_24, Male_25_29, Female_25_29, Male_30_34, Female_30_34, Male_35_39, Female_35_39, Male_40_44, Female_40_44, Male_45_49, Female_45_49, Male_50_54, Female_50_54, Male_55_59, Female_55_59, Male_60_64, Female_60_64, Male_65_69, Female_65_69, Male_70_74, Female_70_74, Male_75_79, Female_75_79, Male_80_84, Female_80_84, Male_85_plus, Female_85_plus) VALUES( 'EuropeanStandard', '8000', '8000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '7000', '6000', '6000', '5000', '5000', '4000', '4000', '3000', '3000', '2000', '2000', '1000', '1000', '1000', '1000')"; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute() or die $dbh->errstr; $dbh->commit();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://310293]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found