#!/usr/bin/env ruby # coding: utf-8 Encoding.default_external = 'utf-8' # UTF-8のCSVファイルを読むため require 'cgi' require 'csv' require 'kakasi' require 'sqlite3' c = CGI.new(:accept_charset => "UTF-8") csv = CSV.read("gomi.csv", headers:true) db = SQLite3::Database.new("tmpdb/gomi.sq3") db.execute_batch(<<~EOF) DROP TABLE IF EXISTS gomi; CREATE TABLE IF NOT EXISTS gomi(name text,type text,remark text,kana text,kata text,roma text); BEGIN; EOF csv.each do |row| values = row.collect{|x| x[-1]} values << Kakasi.kakasi('-JH -KH',row["品名"]) # 酒田市 → さかたし values << Kakasi.kakasi('-JK -HK',row["品名"]) # さかたし → サカタシ values << Kakasi.kakasi('-Ja -Ha -Ka',row["品名"]) # さかたし → sakatashi db.execute("INSERT INTO gomi VALUES(?,?,?,?,?,?)", *values) trash = c["gomi"] db.results_as_hash = true result = db.execute("SELECT * FROM gomi where name||kana||kata||roma LIKE '%#{trash}%';") print ("Content-type: text/html; charset=utf-8") print ("<!DOCTYPE html> <html> <head><title>検索結果</title> </head> <body>") result.each{|row| printf("<br>品名 : 「%s」<br>ごみ区分 : 「%s」 <br> <br> 備考 : %s<br>-------------------------------------------------------------------------------",row["name"], row["type"], row["remark"])} print "</body>\n" print "</html>" end db.execute("END;")