I am importing data from a ':' separated file into a MySQL database. I have split the different elements into an array, but my INSERT statement results in the following error:
DBD::mysql::st execute failed: called with 26 bind variables when 24 a +re needed at csv2sql.pl line 18, <GLEANDATA> line 193.
I know that some fields in the text file are empty (e.g. 'foo:bar::bash'). The split should result in an array like this:
("foo","bar","","bash")
Instead, DBI seems to ignore the "" and only attempts to insert foo, bar, and bash.
How do I insert everything, including undefined values?
Perl:
#!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect("DBI:mysql:db;host=host.com", 'user', 'pw') or die ("Could not connect: " . DBI->errstr ); open GLEANDATA, 'gleandata.csv' or die "Could not open file\n"; my @row; my $sth; my $line; while (<GLEANDATA>) { $line = $_; my @row = split(/:/, $line); $sth = $dbh->prepare("INSERT INTO glean_data (gleaner, contact_nam +e, title_position, city, county, phone, email, dates_called, possible +_donation, notes_comments, followup_needed, mailing_address, physical +_address, crops, specific_crop, estimated_quantity, estimated_labor, +possible_glean_dates_times, ladders_to_lend, picking_bags_to_lend, bi +ns_or_boxes, existing_relationship, status, time_of_year_to_call) VAL +UES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); $sth->execute(@row); } close GLEANDATA or die "Could not close file\n"; $dbh->disconnect;
Just so you know, there are 24 columns I am trying to insert into.
SQL:
CREATE TABLE glean_data ( id MEDIUMINT NOT NULL AUTO_INCREMENT, gleaner VARCHAR(50), contact_name VARCHAR(50), title_position VARCHAR(50), city VARCHAR(50), county VARCHAR(50), phone VARCHAR(20), email VARCHAR(50), dates_called VARCHAR(200), possible_donation VARCHAR(150), notes_comments VARCHAR(5000), followup_needed VARCHAR(50), mailing_address VARCHAR(100), physical_address VARCHAR(100), crops VARCHAR(150), specific_crop VARCHAR(150), estimated_quantity VARCHAR(150), estimated_labor VARCHAR(150), possible_glean_dates_times VARCHAR(500), ladders_to_lend VARCHAR(150), picking_bags_to_lend VARCHAR(150), bins_or_boxes VARCHAR(50), existing_relationship VARCHAR(150), status VARCHAR(150), time_of_year_to_call VARCHAR(50), PRIMARY KEY (id) )
Thank you for your help.
In reply to MySQL: trouble inserting an array with some undefined members into a table using a set number of placeholders by dwstultz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |