2019-03-31 11:00:21 +00:00
---
title: 検索した店舗情報をデーターベースに登録するアクションを作る
author: kazu634
date: 2009-11-18
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4935;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Emacs
---
< div class = "section" >
< p >
< a href = "http://d.hatena.ne.jp/sirocco634/20091118#1258549881" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091118#1258549881', '上記のelisp');" target = "_blank" > 上記のelisp< / a > を改良し、取得した店舗情報をデーターベースに登録するアクションを追加します。
< / p >
< p >
説明には次のようにあります:
< / p >
< blockquote title = "http" cite = "http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything&.submit=%B8%A1%BA%F7&type=detail" >
< dl >
< dt >
action (type 要素が指定されなかった場合、必須)
< / dt >
< dd >
action 要素は「(表示文字列 . 関数)ペアー」のリストである。指定された関数は選択された候補を引数として呼び出される。< br / > 現在選択されている候補のためのデフォルト以外のアクションがこのリストから選ばれる(デフォルトでは TAB キーで)。 DISPLAY 文字列は、補完バッファーで表示され、アクションが選択されたときに関数が呼び出される。リスト中の先頭ののアクションがデフォルト・アクションになる。
< / dd >
< / dl >
< p >
< cite > < a href = "http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything&.submit=%B8%A1%BA%F7&type=detail" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything&.submit=%B8%A1%BA%F7&type=detail', 'http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything&.submit=%B8%A1%BA%F7&type=detail:title');" target = "_blank" > http://d.hatena.ne.jp/sirocco634/searchdiary?word=anything& .submit=%B8%A1%BA%F7& type=detail:title< / a > < / cite >
< / p >
< / blockquote >
< p >
二つ目のアクションとしてデーターベースに登録する関数を割り当てます。データーベースは mysql を使うことにします。コマンドののたたき方は「< a href = "http://d.hatena.ne.jp/sirocco634/20091111#1257945211" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091111#1257945211', '2009-11-11 – 武蔵の日記');" target = "_blank" > 2009-11-11 – 武蔵の日記< / a > 」を参考にします。
< / p >
< h4 >
mysql の設定
< / h4 >
< p >
このようなテーブルを定義しました:
< / p >
< pre class = "syntax-highlight" >
mysql> < span class = "synSpecial" > desc< / span > lunch;
+< span class = "synComment" > ----------+--------------+------+-----+---------+----------------+< / span >
| Field | < span class = "synSpecial" > Type< / span > | < span class = "synSpecial" > Null< / span > | Key | < span class = "synSpecial" > Default< / span > | Extra |
+< span class = "synComment" > ----------+--------------+------+-----+---------+----------------+< / span >
| id | int(< span class = "synConstant" > 11< / span > ) | NO | PRI | < span class = "synSpecial" > NULL< / span > | auto_increment |
| shopname | < span class = "synType" > varchar< / span > (< span class = "synConstant" > 100< / span > ) | NO | | < span class = "synSpecial" > NULL< / span > | |
| address | < span class = "synType" > varchar< / span > (< span class = "synConstant" > 150< / span > ) | NO | | < span class = "synSpecial" > NULL< / span > | |
| tel | < span class = "synType" > varchar< / span > (< span class = "synConstant" > 20< / span > ) | NO | | < span class = "synSpecial" > NULL< / span > | |
| url | < span class = "synType" > varchar< / span > (< span class = "synConstant" > 100< / span > ) | YES | | < span class = "synSpecial" > NULL< / span > | |
| < span class = "synStatement" > comment< / span > | < span class = "synType" > varchar< / span > (< span class = "synConstant" > 500< / span > ) | YES | | < span class = "synSpecial" > NULL< / span > | |
| lat | < span class = "synType" > float< / span > (< span class = "synConstant" > 10< / span > ,< span class = "synConstant" > 7< / span > ) | YES | | < span class = "synSpecial" > NULL< / span > | |
| lng | < span class = "synType" > float< / span > (< span class = "synConstant" > 10< / span > ,< span class = "synConstant" > 7< / span > ) | YES | | < span class = "synSpecial" > NULL< / span > | |
+< span class = "synComment" > ----------+--------------+------+-----+---------+----------------+< / span >
< span class = "synConstant" > 8< / span > < span class = "synSpecial" > rows< / span > < span class = "synStatement" > in< / span > < span class = "synStatement" > set< / span > (< span class = "synConstant" > < / span > .< span class = "synConstant" > 00< / span > sec)
< / pre >
< h4 >
ソース
< / h4 >
< p >
anything-c-source-gourmetを次のように変更します:
< / p >
< pre class = "syntax-highlight" >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > defvar< / span > anything-c-source-gourmet
< span class = "synSpecial" > '((< / span > name . < span class = "synConstant" > " Cross-Gourmet Site Search" < / span > < span class = "synSpecial" > )< / span >
< span class = "synSpecial" > (< / span > candidates . < span class = "synSpecial" > (< / span > < span class = "synStatement" > lambda< / span > < span class = "synSpecial" > ()< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > delete< / span > < span class = "synConstant" > " " < / span >
< span class = "synSpecial" > (< / span > split-string
< span class = "synSpecial" > (< / span > shell-command-to-string
< span class = "synSpecial" > (< / span > < span class = "synStatement" > format< / span > < span class = "synConstant" > " gourmet %s" < / span >
anything-c-source-gourmet-work< span class = "synSpecial" > ))< / span >
< span class = "synConstant" > " \n" </ span >< span class = "synSpecial" > ))))</ span >
< span class = "synSpecial" > (< / span > candidate-transformer . < span class = "synSpecial" > (< / span > < span class = "synStatement" > lambda< / span > < span class = "synSpecial" > (< / span > candidates< span class = "synSpecial" > )< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > mapcar< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > function< / span > < span class = "synSpecial" > (< / span > < span class = "synStatement" > lambda< / span > < span class = "synSpecial" > (< / span > arg< span class = "synSpecial" > )< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > let*< / span >
< span class = "synSpecial" > ((</ span > str_list < span class = "synSpecial" > (</ span > split-string arg < span class = "synConstant" > " \t" </ span >< span class = "synSpecial" > ))</ span >
< span class = "synSpecial" > (< / span > site < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > 0 str_list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > shopname < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > 1 str_list< span class = "synSpecial" > )))< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > cons< / span > < span class = "synSpecial" > (< / span > < span class = "synStatement" > format< / span > < span class = "synConstant" > " [%s] %s" < / span > site shopname< span class = "synSpecial" > )< / span > arg< span class = "synSpecial" > ))))< / span >
candidates< span class = "synSpecial" > )))< / span >
< span class = "synSpecial" > (< / span > action . < span class = "synSpecial" > ((< / span > < span class = "synConstant" > " Insert" < / span > . insert_gourmet< span class = "synSpecial" > )< / span >
< span class = "synSpecial" > (< / span > < span class = "synConstant" > " Database" < / span > . insert_gourmet_database< span class = "synSpecial" > )))< / span >
< span class = "synSpecial" > (< / span > migemo< span class = "synSpecial" > )))< / span >
< / pre >
< p >
また、関数 insert_gourmet_database を次のように定義します:
< / p >
< pre class = "syntax-highlight" >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > defun< / span > insert_gourmet_database < span class = "synSpecial" > (< / span > candidate< span class = "synSpecial" > )< / span >
< span class = "synSpecial" > (< / span > < span class = "synStatement" > let*< / span >
< span class = "synSpecial" > ((</ span > str-list < span class = "synSpecial" > (</ span > split-string candidate < span class = "synConstant" > " \t" </ span >< span class = "synSpecial" > ))</ span >
< span class = "synSpecial" > (< / span > site < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > < span class = "synConstant" > < / span > str-list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > shopname < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > < span class = "synConstant" > 1< / span > str-list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > address < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > < span class = "synConstant" > 2< / span > str-list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > url < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > < span class = "synConstant" > 3< / span > str-list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > tel < span class = "synSpecial" > (< / span > < span class = "synStatement" > nth< / span > < span class = "synConstant" > 4< / span > str-list< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > comments < span class = "synSpecial" > (< / span > read-string < span class = "synConstant" > " Comments, if you have any: " < / span > < span class = "synConstant" > " " < / span > < span class = "synStatement" > nil< / span > < span class = "synConstant" > " " < / span > < span class = "synSpecial" > )))< / span >
< span class = "synSpecial" > (< / span > call-process-shell-command
< span class = "synSpecial" > (< / span > < span class = "synStatement" > format< / span >
< span class = "synConstant" > " echo 'INSERT INTO lunch (shopname, address, tel, url, comment) values (\" ;%s\" ;, \" ;%s\" ;, \" ;%s\" ;, \" ;%s\" ;, \" ;%s\" ;);' | mysql5 -uusr -ppassword -h srv634 shop" </ span >
shopname address tel url comments< span class = "synSpecial" > ))< / span >
< span class = "synSpecial" > (< / span > message < span class = "synConstant" > " Inserting DB finished." < / span > < span class = "synSpecial" > )))< / span >
< / pre >
< p >
私は店舗情報を登録するときにコメント用のフィールドにコメントを残したかったので、コメントの入力を促すようにしました。
< / p >
< h4 >
「anything」に関連する最近のエントリ
< / h4 >
< ul >
< li >
< a href = "http://d.hatena.ne.jp/sirocco634/20091112/1258032660" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091112/1258032660', ' リストの n 番目を取り出す – 武蔵の日記');" target = "_blank" > リストの n 番目を取り出す – 武蔵の日記< / a >
< / li >
< li >
< a href = "http://d.hatena.ne.jp/sirocco634/20091101/1257084590" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091101/1257084590', ' dummy の使い方 – 武蔵の日記');" target = "_blank" > dummy の使い方 – 武蔵の日記< / a >
< / li >
< li >
< a href = "http://d.hatena.ne.jp/sirocco634/20091029/1256826524" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091029/1256826524', ' 書き捨てのソース – 武蔵の日記');" target = "_blank" > 書き捨てのソース – 武蔵の日記< / a >
< / li >
< li >
< a href = "http://d.hatena.ne.jp/sirocco634/20091028/1256742357" onclick = "__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20091028/1256742357', ' anything の source を作ってみる (initの使い方) – 武蔵の日記');" target = "_blank" > anything の source を作ってみる (initの使い方) – 武蔵の日記< / a >
< / li >
< / ul >
2019-04-02 16:06:15 +00:00
< / div >