以前、仕事の関係で特定健診のXMLファイルからCSVへ変換するC#プログラムを書きました。 直近、研究のため、WHO薬用植物の本から概念を抽出してXMLに書き込んだり、読み取ったりするPHPプログラムも作りました。 最近、AppleScript で 脆弱性検査結果の XML ファイルの処理をしようと考えています。 まずは、情報集めですね。 AppleScript は中級者のものといわれて、あまり本とか情報がないようです。 DOM で XML を取り扱うのは、いくつかのサイトにはありますが、とりあえず フランスの SATIMAGE とういうサイトを中心に参考します。 フランスのサイトですが、幸い英語のページも用意されています。 トップページに、SmileLab と Smile の2つのソフトが紹介されて、データの可視化とワークフローの自動化のツールですが、 Smile のほうが XMLList osax を利用しています。XMLList osax こそ AppleScript での XML DOM の実装であります。 SATIMAGEでは、XMLList の辞書も提供されています。 XMLList のダウンロードは 下記の2つがありますが、なぜか Build 373 のリンクが切れているようです。 XMLLib osax 3.7.0 (build 373) XMLLib osax 3.7.0 (build 370) 余裕があれば、少しずつサンプルなり追加記事をしていきます。 shyunsei.blogspot.jp 関連サイト: XPath -Tutorial
Category Archive: データベース
php で楽々 sqlite を管理するツール phpliteadmin.php

https://code.google.com/p/phpliteadmin/ sqliteはサーバが不要で、一つのファイルで一つのファイルで納まるため、殆どのモバイルディバイスに利用されています。 もちろん、編集や管理するため、いろいろなアプリがありますが、サーバに管理ツールがあれば、プラットフォームやディバイスに関係なく、簡単にどこでも自分のデータベースを管理できれば、一番良いのです。 そこでphpliteadminというただプログラム一本でよくできているツールがあります。設定も簡単でサーバにアップロードして同じディレクトリにデータベースファイルを入れて読み取りと書き込みの権限を与えるとすぐ使えます。
root 権限なしで、mysql をソースコードでインストールする(Linux)
まず Mysql のソースコードをこちらからダウンロードします。 Select Platform は、「Source Code」を選んで、Generic Linux (Architecture Independent), Compressed TAR Archive をダウンロードしてください。(今回ダウンロードしたのはmysql-5.6.5-m8.tar.gzです)
1 2 3 4 5 6 7 8 9 10 11 |
tar xvfz mysql-5.6.5-m8.tar.gz cd mysql-5.6.5-m8 cmake . -DCMAKE_INSTALL_PREFIX=/home/yourname/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/home/yourname/tmp/mysql55.sock -DMYSQL_TCP_PORT=23306 make make install cd .. ./mysql/scripts/mysql_install_db --basedir=/home/yourname/mysql --datadir=/home/yourname/mysql/data --user=yourname cp mysql/support-files/my-medium.cnf mysql/my.cnf ./mysql/bin/mysqladmin -u root password 'newpasswd' -P 23306 ./mysql/bin/mysqld_safe --defaults-file=/home/yourname/mysql/my.cnf & ./mysql/bin/mysql -u root -P 23306 -p |
停止は下記のコマンドを使います。
1 |
./mysql/bin/mysqladmin -u root -p -P 23306 shutdown |
自宅サーバでVPSサービス?!
アメリカのポストンで不動産の紹介をやっている義理の弟がサイト作りたいので相談しに来ました。 このブログのサーバを貸し出すのもいいのですが、やはり仮想サーバを作って貸し出したほうがいいかなと思って さくらインターネットのVPS 1Gのようなスペックで作りました。 Macmini 2011 に Debian のみを稼働させるのもできますが、家は lion server も使いたいので、 VMwareの上で Debian の Xen 仮想サーバをVPSとして作ったのです。ちょっと複雑ですが。。。 LVS+Heartbeat+ldirectordで構築した仮想ロードバランサー2台、Web サーバ1台、database サーバ1台、VPS1台、計5台を稼働させても 8GBのメモリは3.2GBの余裕があります。CPU使用率は10%以下に収まります。 リモートアクセスで Lion server を使ったほうがCPUとメモリのリソースを喰いますね。^^;
perl memo DBD::CSV csv file name
create table mytable.csv ( id text, name text) このようにSQLを実行させた場合、mytable.csv が作られます。 しかし、この拡張子があるファイル名は、後ほどテーブルのJOINにエラーを起こすことがあります。 例えば、 select a.id, b.name from mytable.csv a, yourtable.csv b where a.id=b.id どうしてもcsvの拡張子を使いたい場合、ln -s シンボルリンクを作って、mytable.csv => mytable のようにすれば、使えるようになります。 シンボルリンクを作ってからのSQLは下記のなりますね。 select a.id, b.name from mytable a, yourtable b where a.id=b.id
perl memo DBD::CSV Built-in Functions
DBD::CSV で SQL の関数は何が使えるか?ずっと疑問を持っています。 答えは、SQL::Statement::Functions を利用するので、 こちらの Built-in Functions が使えるそうです。 多くありませんね。 min, max, avg, sum, count current_date, current_time, current_timestamp char_length, lower, position, regex, soundex, substring, trim, upper それから、使いたいのが length ですが、char_length しかありませんね。 今度、User-Defined Functions に挑戦してみようと。
perl memo DBD::CSV LEFT JOIN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#!/usr/bin/perl use DBI; $dbh = DBI->connect("DBI:CSV:f_dir=/Volumes/RamDisk/csvdb") or die "Cannot connect: " . $DBI::errstr; $sth = $dbh->prepare("DROP TABLE a ") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("CREATE TABLE a (id INTEGER not null, name CHAR(10))") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into a (id,name) values (1,'カンマ 太郎')") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into a (id,name) values (2,'カンマ 次郎')") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" update a set name='カンマ 花子' where id=1") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("DROP TABLE b ") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("CREATE TABLE b (id INTEGER not null, age INTEGER)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into b (id,age) values (2,30)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into b (id,age) values (3,26)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into b (id,age) values (4,27)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); my($query) = "SELECT a.id id , a.name name, b.age age FROM a left join b on a.id = b.id"; my($sth) = $dbh->prepare($query); $sth->execute(); while (my $row = $sth->fetchrow_hashref) { print("Found result row: id = ", $row->{'id'}, ", name = ", $row->{'name'}, ", age = ", $row->{'age'}, "\n"); } $sth->finish(); $dbh->disconnect(); |
【実行結果】 My-iPad:~/textastic/perl root# perl csv_left_join.pl Found result row: id = 1, name = カンマ 花子, age = Found result row: id = 2, name = カンマ 次郎, age = 30
perl memo DBD::CSV JOIN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#!/usr/bin/perl use DBI; $dbh = DBI->connect("DBI:CSV:f_dir=/Volumes/RamDisk/csvdb") or die "Cannot connect: " . $DBI::errstr; $sth = $dbh->prepare("DROP TABLE a ") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("CREATE TABLE a (id INTEGER not null, name CHAR(10))") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into a (id,name) values (1,'カンマ 太郎')") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into a (id,name) values (2,'カンマ 次郎')") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" update a set name='カンマ 花子' where id=1") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("DROP TABLE b ") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("CREATE TABLE b (id INTEGER not null, age INTEGER)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into b (id,age) values (2,30)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare(" insert into b (id,age) values (1,26)") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); my($query) = "SELECT a.id id , a.name name, b.age age FROM a a, b b WHERE a.id = b.id"; my($sth) = $dbh->prepare($query); $sth->execute(); while (my $row = $sth->fetchrow_hashref) { print("Found result row: id = ", $row->{'id'}, ", name = ", $row->{'name'}, ", age = ", $row->{'age'}, "\n"); } $sth->finish(); $dbh->disconnect(); |
【実行結果】 My-iPad:~/textastic/perl root# perl csv_join.pl Found result row: id = 1, name = カンマ 花子, age = 26 Found result row: id = 2, name = カンマ 次郎, age = 30
perl memo DBD::CSVを使ってみる
DBD::CSVはCPANのページを御覧ください。それから、日本語訳はこちらです。 iPadのインストールとMacBook pro のインストールは前の記事を御覧ください。 なお、基本的に多言語の対応のデータ処理を想定しておりますので、文字コードはUTF-8でやっております。 まず、テーブルの削除(DROP)と作成(CREATE)、そしてデータの挿入(INSERT)と更新(UPDATE)をやってみましょう。 (PRIMARY KEY は使えないそうです) 以下はサンプルcsv_as_db.plの中身です。 #!/usr/bin/perl use DBI; $dbh = DBI->connect("DBI:CSV:f_dir=/Volumes/RamDisk/csvdb") or die "Cannot connect: " . $DBI::errstr; $sth = $dbh->prepare("DROP TABLE a.csv ") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); $sth = $dbh->prepare("CREATE TABLE a.csv (id INTEGER not null, name CHAR(10))") or die ...
postgresql memo 異なるスキーマの突合
--検索スキーマの優先順位を設定 set search_path to schema1,schema2; --いまのスキーマを表示 select current_schema(); --異なるスキーマにある同じ名前のテーブルを突合 select t1.*,t2.* from schema1.table1 s1, schema2.info_resource s2 where s1.key=s2.key;