Here's some suggestions to try:
my $ins_sth = $dbh->prepare('INSERT INTO hobby (id,descr,item,latin) V
+ALUES (?,?,?,?)')|| die $dbh->errstr;
my $csvfile="/home/bradley/dbtest/item_instk.csv";
my $id = 1;
open (CSV,"$csvfile");
while (<CSV>) {
chomp;
($item,$descr,$latin) = split('/',$_);
$ins_sth->execute( $id, $descr, $item, $latin )||die $dbh->err
+str;
++$id;
}
close CSV;
-
I've never used bind_param, and as far as I can tell, it's completely unnecessary for what you are trying to do.
- setting $id to 1 inside the while loop will make all of your id's be set to 1 when the data is inserted. Further I suggest that you not use $id at all. Databases have built in functions for generating unique id's and it's much safer to use them than to make them yourself.
- I wouldn't call my statement handle 'select' if it's actually doing an insert. 'sth' is what the perldocs use in the examples...perhaps 'ins_sth' is a more mnemonic name.
you need to call 'finish' on a statement handle after you are done using it.
- change 'chop' to 'chomp', 'chomp' only removes newlines, and if the last line of your data file doesn't have one, 'chop' would take off the last character of $latin.
I realize that this isn't a direct answer to your question, but I hope that it helps you make things go more smoothly.
Update:Thanks for the corrections runrig
/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.