added mysql/load-data.sh

pull/4/head
agentzh (章亦春) 13 years ago
parent abfff86dbf
commit 56e75e09f2
  1. 2
      demo/Blog/misc/mysql/init-db.sql
  2. 12
      demo/Blog/misc/mysql/load-data.sh
  3. 12
      util/import-model.pl

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

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

Loading…
Cancel
Save