簡體   English   中英

使用Perl從CSV到MySQL數據庫

[英]From CSV to MySQL Database Using Perl

我試圖將一些數據從CSV上傳到MySQL數據庫 - 但它不起作用

下面是我的代碼

#!/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;

這里有一些問題。 我將列出那些首先會給你錯誤信息的。

  • 沒有模塊TEXT :: CSV。 有一個名為Text :: CSV但是。
  • 您在查詢中使用了5個占位符,但是您通過菱形運算符<CSV>傳遞了csv文件的第一行。 這將給出錯誤消息。

然后你的邏輯有問題。 您正在將完整文件傳遞給DB(作為第一個參數)。 那沒有意義。 您需要split輸入或使用Text :: CSV來執行此操作並逐行讀取文件。

此外,現在最好使用帶有三個參數的open並使文件句柄有詞匯。

我已經把所有這些都寫成了自制CSV處理的例子。 如果您的文件更復雜,請閱讀Text :: CSV並使用它。

use DBI;
use strict;
use warnings;

my $csv          = "C:/Perl/scripts/table.csv";
# omitted settings here ...

my $dbh = DBI->connect($dsn, $userid, $password,{ RaiseError => 1})
  or die "Could not connect to database! $DBI::errstr";
open (my $fh, '<', $csv) 
  or die "Couldn't open csvfile: $!";

# prepare statement handle for reuse in the loop
my $sth = $dbh->prepare(qq{
  INSERT INTO student(stud_id,stud_name,dept_id,stud_mark,stud_address) 
  VALUES (?, ?, ?, ?, ?)});

# read the file line by line
while (my $line = <$fh>) {
  chomp $line; # remove newline
  $sth->execute( split /;/, $line ); # assuming the separator is a semicolon 
}

close $fh;
# DB handle will disconnect implicitly on end of program

正如您所看到的,我決定事先准備好聲明並重新使用它。 這樣可以在循環中節省大量時間,因為DB會記住該語句。

從列表上下文中的文件句柄(即代碼中的<CSV>位)讀取文件中的所有行並將其作為列表返回。 所以你的?, ?, ?, ? 占位符每個都從文件中獲得一整行(包括末尾的換行符)。 對於某些字段(可能是dept_id ?),這可能不是有效值,因此INSERT語句失敗。

實際上,你也設置$/到undef,這使得它甚至是wrongerer。 $/更改Perl在讀取文本文件時對新行的概念。 將其設置為undef意味着Perl會將整個文件視為一行。

猜測一下,您要做的是一次讀取一行CSV文件,並將每個文件泵入數據庫。

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Text::CSV;  # case-sensitive!

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";

# Connect to database.
my $dsn = "dbi:mysql:dbname=$databasename;host=$dbhost;port=$dbport;";
my $dbh = DBI->connect($dsn, $userid, $password,{ RaiseError => 1})
  or die "Could not connect to database! $DBI::errstr";

# DBI can be more efficient if you prepare the SQL query once, and then
# execute it multiple times, rather than calling `do` for each insert.
my $sth = $dbh->prepare(<<'SQL');
  INSERT INTO student (stud_id,stud_name,dept_id,stud_mark,stud_address)
  VALUES (NULL, ?, ?, ?, ?)"
SQL

# Open the CSV file.A
open my $CSV, '<', $csv
  or die "Couldn't open csvfile: $!";

# Create an instance of Text::CSV.
my $reader = Text::CSV->new;

# Use Text::CSV to read a line.
while (my $row = $reader->getline($CSV))
{
  # Insert into database.
  $sth->execute( @$row );
}

# Clean up (optional; Perl will do this when your script ends anyway).
$dbh->disconnect;
close $CSV;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM