0

i tried to upload some data from csv to MySql data base. but it not working

someone help me on this. the below one is my code

#!/usr/bin/perl -w
use DBI;
use strict;
use TEXT::CSV;
use warnings;

my $driver       = "mysql"; 
my $database     = "test";
my $host         = "localhost"
my $databaseport = "3306";
my $userid       = "root";
my $password     = "password";
my $csv          = "C:/Perl/scripts/table.csv";

my $dsn          = "dbi:mysql:dbname=$databasename;host=$dbhost;port=$dbport;";

open (CSV, "$csv") or die "Couldn't open csvfile: $!";
my $dbh = DBI->connect($dsn, $userid, $password,{ RaiseError => 1})
or die "Could not connect to database! $DBI::errstr";
{ 
 local $/ = undef; 
  $dbh->do("INSERT INTO student (stud_id,stud_name,dept_id,stud_mark,stud_address) 

  values (?, ?, ?, ?, ?)", undef, <CSV>);
 }
 $dbh->disconnect;
close CSV;

Edited by pritaeas: Moved to perl.

2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by venyz
0

Setting local $/ = undef; means that you’ll read in the complete CSV file as a single string. That is, you only end up with one piece of information, rather than the separate fields you expect.
I suggest using the Text::CSV module to get the contents of the CSV file one record at a time. The module’s POD has examples of how to do this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.