added mysql/load-data.sh

pull/4/head
agentzh (章亦春) 15 years ago
parent abfff86dbf
commit 56e75e09f2

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

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

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

Loading…
Cancel
Save