added mysql/load-data.sh

This commit is contained in:
agentzh (章亦春) 2010-01-20 18:34:27 +08:00
parent abfff86dbf
commit 56e75e09f2
3 changed files with 20 additions and 6 deletions

View File

@ -15,7 +15,7 @@ drop table if exists comments;
create table comments ( create table comments (
id serial, id serial,
sender varchar(64) not null, sender varchar(64) not null,
email varchar(64) not null, email varchar(64),
url varchar(1024), url varchar(1024),
body text not null, body text not null,
created timestamp(0) default now() not null, created timestamp(0) default now() not null,

View File

@ -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

View File

@ -66,7 +66,9 @@ $model or die "No --model given.\n";
my $database = 'test'; 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 }); my $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 });
if ($reset) { if ($reset) {
@ -102,7 +104,7 @@ while (<>) {
my $cols = join ',', @cols; my $cols = join ',', @cols;
my @holders = map { '?' } @cols; my @holders = map { '?' } @cols;
my $holders = join ',', @holders; my $holders = join ',', @holders;
$sth = $dbh->prepare("insert into ($cols) values ($holders)"); $sth = $dbh->prepare("insert into $model ($cols) values ($holders)");
} }
my @vals; my @vals;
@ -113,15 +115,15 @@ while (<>) {
$sth->execute(@vals); $sth->execute(@vals);
$inserted++; $inserted++;
if (@rows % $step == 0) { if ($inserted % $step == 0) {
$inserted += insert_rows(\@rows);
@rows = ();
print STDERR "\r", ($update_col ? "Updated" : "Inserted"), " rows: $inserted (row $.)"; print STDERR "\r", ($update_col ? "Updated" : "Inserted"), " rows: $inserted (row $.)";
} }
} }
print STDERR "\n$inserted row(s) inserted.\n"; print STDERR "\n$inserted row(s) inserted.\n";
$dbh->disconnect();
warn "\nFor tatal $inserted records inserted.\n"; warn "\nFor tatal $inserted records inserted.\n";
#print encode('UTF-8', YAML::Syck::Dump(\@rows)); #print encode('UTF-8', YAML::Syck::Dump(\@rows));