From 56e75e09f2b3097d1ee31c137d7fdfb2cb939103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Wed, 20 Jan 2010 18:34:27 +0800 Subject: [PATCH] added mysql/load-data.sh --- demo/Blog/misc/mysql/init-db.sql | 2 +- demo/Blog/misc/mysql/load-data.sh | 12 ++++++++++++ util/import-model.pl | 12 +++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100755 demo/Blog/misc/mysql/load-data.sh diff --git a/demo/Blog/misc/mysql/init-db.sql b/demo/Blog/misc/mysql/init-db.sql index 3a98351..70bf2f7 100644 --- a/demo/Blog/misc/mysql/init-db.sql +++ b/demo/Blog/misc/mysql/init-db.sql @@ -15,7 +15,7 @@ drop table if exists comments; create table comments ( id serial, sender varchar(64) not null, - email varchar(64) not null, + email varchar(64), url varchar(1024), body text not null, created timestamp(0) default now() not null, diff --git a/demo/Blog/misc/mysql/load-data.sh b/demo/Blog/misc/mysql/load-data.sh new file mode 100755 index 0000000..bb50d64 --- /dev/null +++ b/demo/Blog/misc/mysql/load-data.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +mysql -h localhost -p -u monty test < misc/mysql/init-db.sql + +../../util/import-model.pl --reset --user monty \ + --password some_pass --server localhost \ + --model posts Post.json + +../../util/import-model.pl --reset --user monty \ + --password some_pass --server localhost \ + --model comments Comment.json + diff --git a/util/import-model.pl b/util/import-model.pl index 7815e18..fa90d35 100755 --- a/util/import-model.pl +++ b/util/import-model.pl @@ -66,7 +66,9 @@ $model or die "No --model given.\n"; my $database = 'test'; -my $dsn = "DBI:mysql:database=$database;host=$server;port=$port"; +$port ||= 3306; + +my $dsn = "DBI:mysql:database=$database;host=$server;port=$port;mysql_server_prepare=1"; my $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 }); if ($reset) { @@ -102,7 +104,7 @@ while (<>) { my $cols = join ',', @cols; my @holders = map { '?' } @cols; my $holders = join ',', @holders; - $sth = $dbh->prepare("insert into ($cols) values ($holders)"); + $sth = $dbh->prepare("insert into $model ($cols) values ($holders)"); } my @vals; @@ -113,15 +115,15 @@ while (<>) { $sth->execute(@vals); $inserted++; - if (@rows % $step == 0) { - $inserted += insert_rows(\@rows); - @rows = (); + if ($inserted % $step == 0) { print STDERR "\r", ($update_col ? "Updated" : "Inserted"), " rows: $inserted (row $.)"; } } print STDERR "\n$inserted row(s) inserted.\n"; +$dbh->disconnect(); + warn "\nFor tatal $inserted records inserted.\n"; #print encode('UTF-8', YAML::Syck::Dump(\@rows));