{"id":276,"date":"2016-11-14T06:57:25","date_gmt":"2016-11-13T21:57:25","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=276"},"modified":"2018-10-18T00:51:07","modified_gmt":"2018-10-17T15:51:07","slug":"blog17","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog17\/","title":{"rendered":"(17) SQLite\u3067\u7c21\u6613\u63b2\u793a\u677f\u4f5c\u6210"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u307e\u305a\u306f\u300c\u6295\u7a3f\u300d\u3001\u300c\u7de8\u96c6\u300d\u3001\u300c\u524a\u9664\u300d\u3092\u5b9f\u88c5\u3059\u308b\u3002<br \/>\n\u753b\u9762\u30ec\u30a4\u30a2\u30a6\u30c8\u306f index.php \u306b\u66f8\u304f\u3088\u3046\u306b\u5207\u308a\u96e2\u3057\u3066\u3042\u308b\u306e\u3067\u3001\u30c7\u30b6\u30a4\u30f3\u304d\u308c\u3044\u5316\u306f\u5f8c\u3067&#8230;<\/p>\n<p>\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/www.dogrow.net\/php\/sample\/00017\/\" target=\"_blank\">http:\/\/www.dogrow.net\/php\/sample\/00017\/<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/11\/Image1-1.gif\" class=\"my_img\" \/><\/a><\/p>\n<h1 class=\"my_h\">2. \u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210<\/h1>\n<h3 class=\"my_h\">1) index.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot; \/&gt;\r\n&lt;title&gt;SQLite\u30b5\u30f3\u30d7\u30eb&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;\/body&gt;\r\n&lt;?php\r\n  require_once(&quot;CBbsMng.php&quot;);\r\n  $cBbsMng = new CBbsMng();\r\n  $cBbsMng-&gt;connect_db();\r\n\r\n  \/\/ \u6295\u7a3f\u5236\u5fa1\r\n  $cmd =(isset($_POST&#x5B;'cmd']))? $_POST&#x5B;'cmd'] : 0;\r\n  if($cmd == 1){\r\n    $tid   = htmlspecialchars($_POST&#x5B;'tid']);\r\n    $title = htmlspecialchars($_POST&#x5B;'title']);\r\n    $name  = htmlspecialchars($_POST&#x5B;'name']);\r\n    $bun   = htmlspecialchars($_POST&#x5B;'bun']);\r\n    if(($title == &quot;&quot;) &amp;&amp; ($name == &quot;&quot;) &amp;&amp; ($bun == &quot;&quot;)){\r\n      if($tid == 0){      \/\/ \u65b0\u898f\u30b9\u30ec\u30c3\u30c9\u3092\u4f5c\u6210\uff1f\r\n        $cBbsMng-&gt;create_thread($title, $name, $bun);\r\n      }else{\r\n        $cBbsMng-&gt;create_item($tid, $title, $name, $bun);\r\n      }\r\n    }\r\n  }\r\n  \/\/ \u30b9\u30ec\u30c3\u30c9\u8868\u793a\r\n  $ary_th = $cBbsMng-&gt;get_thread();\r\n  foreach($ary_th as $th){\r\n    echo &quot;&lt;hr&gt;&lt;h1 style=\\&quot;color:#f00\\&quot;&gt;&quot;.$th&#x5B;'title'].&quot;&lt;\/h1&gt;&quot;;\r\n    $ary_it = $cBbsMng-&gt;get_item($th&#x5B;'tid']);\r\n    foreach($ary_it as $it){\r\n      echo &quot;&lt;h2&gt;\u25cf&quot;.$it&#x5B;'title'].&quot; &#x5B;&quot;.$it&#x5B;'name'].&quot;] &quot;.strftime(&quot;%G\/%m\/%d %H:%M:%S&quot;,$it&#x5B;'dt_post']).&quot;&lt;\/h2&gt;&quot;;\r\n      echo &quot;&lt;p style=\\&quot;border:1px #000 solid;margin-left:2rem;padding:0.5rem;\\&quot;&gt;&quot;.$it&#x5B;'bun'].&quot;&lt;\/p&gt;&quot;;\r\n    }\r\n  }\r\n?&gt;\r\n\r\n&lt;hr&gt;\r\n&lt;form enctype=&quot;multipart\/form-data&quot; action=&quot;.\/&quot; method=post&gt;\r\n\u30b9\u30ec\u30c3\u30c9\uff1a&lt;select name=tid&gt;\r\n&lt;option value=0&gt;\u65b0\u898f\u4f5c\u6210&lt;\/option&gt;\r\n&lt;?\r\n  foreach($ary_th as $th){\r\n    echo &quot;&lt;option value=\\&quot;&quot;.$th&#x5B;'tid'].&quot;\\&quot;&gt;&quot;.$th&#x5B;'title'].&quot;&lt;\/option&gt;&quot;;\r\n  }\r\n?&gt;\r\n&lt;\/select&gt;&lt;br \/&gt;\r\n&lt;input type=hidden name=cmd value=1&gt;\r\n\u30bf\u30a4\u30c8\u30eb\uff1a&lt;input type=text name=title&gt;&lt;br \/&gt;\r\n\u540d\u524d\uff1a&lt;input type=text name=name&gt;&lt;br \/&gt;\r\n\u8a18\u4e8b\uff1a&lt;textarea name=bun&gt;&lt;\/textarea&gt;&lt;br \/&gt;\r\n&lt;input type=submit value=&quot;\u6295\u7a3f&quot;&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3 class=\"my_h\">2) CBbsMng.php<\/h3>\n<p><span class=\"my_fc_crimson\">\u203bthread\u3068 item\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u5206\u3051\u308b\u5fc5\u8981\u306f\u306a\u304b\u3063\u305f&#8230; \u3044\u3064\u304b\u4e00\u3064\u306b\u307e\u3068\u3081\u3088\u3046\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nclass CBbsMng {\r\n  private $m_db;        \/\/ SQLite3 instance\r\n  private $m_db_fpath;  \/\/ DB\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\r\n\r\n  function __construct( $fpath = &quot;thbbs.db&quot; ) {\r\n    $this-&gt;m_db = NULL;\r\n    $this-&gt;m_db_fpath = $fpath;\r\n  }\r\n\r\n  \/\/ DB\u63a5\u7d9a\r\n  public function connect_db(){\r\n    if($this-&gt;m_db == NULL){\r\n      $isDbExist = file_exists($this-&gt;m_db_fpath);\r\n      \/\/ DB\u63a5\u7d9a\r\n      $this-&gt;m_db = new SQLite3($this-&gt;m_db_fpath);\r\n      $this-&gt;m_db-&gt;enableExceptions(TRUE);\r\n      if($isDbExist === FALSE){   \/\/ DB\u306f\u65b0\u898f\u4f5c\u6210\uff1f\r\n        try {\r\n          \/\/ \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\r\n          $this-&gt;m_db-&gt;exec('begin');\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `thread` (`tid` INTEGER PRIMARY KEY, `top_iid` INTEGER, `new_iid` INTEGER, `dt_update` INTEGER);&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `item`   (`iid` INTEGER PRIMARY KEY, `tid` INTEGER, `title` TEXT, `name` TEXT, `bun` TEXT, `dt_post` INTEGER);&quot;);\r\n          $this-&gt;m_db-&gt;exec('commit');\r\n        }catch(Exception $e){\r\n          $this-&gt;m_db-&gt;exec('rollback');\r\n          echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ \u30b9\u30ec\u30c3\u30c9\u3092\u4f5c\u6210\r\n  public function create_thread( $title, $name, $bun ){\r\n    if($this-&gt;m_db != NULL){\r\n      $dt_now = time();\r\n      try {\r\n        $this-&gt;m_db-&gt;exec('begin');\r\n        \/\/ \u30bf\u30a4\u30c8\u30eb\u3092\u4f5c\u6210\r\n        $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `thread` (`top_iid`,`new_iid`,`dt_update`) VALUES('0','0','0');&quot;);\r\n        $tid = $this-&gt;m_db-&gt;lastInsertRowID();\r\n        \/\/ \u8a18\u4e8b\u3092\u4f5c\u6210\r\n        $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `item` (`tid`,`title`,`name`,`bun`,`dt_post`) VALUES('$tid','$title','$name','$bun','$dt_now');&quot;);\r\n        $iid = $this-&gt;m_db-&gt;lastInsertRowID();\r\n        \/\/ \u30bf\u30a4\u30c8\u30eb\u306b\u5148\u982d\u9805\u76ee\u306eID\u3092\u6301\u305f\u305b\u308b\u3002\r\n        $this-&gt;m_db-&gt;exec(&quot;UPDATE `thread` SET `top_iid`='$iid',`new_iid`='$iid',`dt_update`='$dt_now' WHERE `tid`='$tid';&quot;);\r\n        $this-&gt;m_db-&gt;exec('commit');\r\n      }catch(Exception $e){\r\n        $this-&gt;m_db-&gt;exec('rollback');\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ \u8a18\u4e8b\u3092\u4f5c\u6210\r\n  public function create_item( $tid, $title, $name, $bun ){\r\n    if($this-&gt;m_db != NULL){\r\n      $dt_now = time();\r\n      try {\r\n        $this-&gt;m_db-&gt;exec('begin');\r\n        $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `item` (`tid`,`title`,`name`,`bun`,`dt_post`) VALUES('$tid','$title','$name','$bun','$dt_now');&quot;);\r\n        $iid = $this-&gt;m_db-&gt;lastInsertRowID();\r\n        $this-&gt;m_db-&gt;exec(&quot;UPDATE `thread` SET `new_iid`='$iid',`dt_update`='$dt_now' WHERE `tid`='$tid';&quot;);\r\n        $this-&gt;m_db-&gt;exec('commit');\r\n      }catch(Exception $e){\r\n        $this-&gt;m_db-&gt;exec('rollback');\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ \u8a18\u4e8b\u3092\u5909\u66f4\r\n  public function modify_item( $iid, $title, $name, $bun ){\r\n    if($this-&gt;m_db != NULL){\r\n      $dt_now = time();\r\n      $rs = $this-&gt;m_db-&gt;query(&quot;SELECT `tid` FROM `item` WHERE `iid`='$iid';&quot;);\r\n      $row = $rs-&gt;fetchArray();\r\n      if(isset($row&#x5B;'tid']) === TRUE){\r\n        try {\r\n          $this-&gt;m_db-&gt;exec('begin');\r\n          $tid = $row&#x5B;'tid'];\r\n          $this-&gt;m_db-&gt;exec(&quot;UPDATE `item` SET `title`='$title',`name`='$name',`bun`='$bun',`dt_post`='$dt_now' WHERE `iid`='$iid';&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;UPDATE `thread` SET `new_iid`='$iid',`dt_update`='$dt_now' WHERE `tid`='$tid';&quot;);\r\n          $this-&gt;m_db-&gt;exec('commit');\r\n        }catch(Exception $e){\r\n          $this-&gt;m_db-&gt;exec('rollback');\r\n          echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ \u30b9\u30ec\u30c3\u30c9\u3092\u524a\u9664\r\n  public function delete_thread( $tid ){\r\n    $ret = array();\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        $this-&gt;m_db-&gt;exec('begin');\r\n        \/\/ \u30b9\u30ec\u30c3\u30c9\u3092\u524a\u9664\r\n        $this-&gt;m_db-&gt;query(&quot;DELETE FROM `thread` WHERE `tid`='$tid';&quot;);\r\n        \/\/ \u914d\u4e0b\u306e\u8a18\u4e8b\u3092\u524a\u9664\r\n        $this-&gt;m_db-&gt;query(&quot;DELETE FROM `item` WHERE `tid`='$tid';&quot;);\r\n        $this-&gt;m_db-&gt;exec('commit');\r\n      }catch(Exception $e){\r\n        $this-&gt;m_db-&gt;exec('rollback');\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n\r\n  \/\/ \u8a18\u4e8b\u3092\u524a\u9664\r\n  public function delete_item( $iid ){\r\n    $ret = array();\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        $this-&gt;m_db-&gt;exec('begin');\r\n        $this-&gt;m_db-&gt;query(&quot;DELETE FROM `item` WHERE `iid`='$iid';&quot;);\r\n        $this-&gt;m_db-&gt;exec('commit');\r\n      }catch(Exception $e){\r\n        $this-&gt;m_db-&gt;exec('rollback');\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n\r\n  \/\/ \u30b9\u30ec\u30c3\u30c9\u3092\u53d6\u5f97\r\n  public function get_thread(){\r\n    $ret = array();\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        \/\/ \u5168\u30b9\u30ec\u30c3\u30c9\u60c5\u5831\u3092\u53d6\u5f97\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT * FROM `thread` ORDER BY `dt_update` DESC;&quot;);\r\n        while($row = $rs-&gt;fetchArray()){\r\n          if(isset($row&#x5B;'tid']) === TRUE){\r\n            \/\/ \uff11\u30b9\u30ec\u30c3\u30c9\u306e\u5148\u982d\u8a18\u4e8b\u3092\u53d6\u5f97\r\n            $rs2 = $this-&gt;m_db-&gt;query(&quot;SELECT * FROM `item` WHERE `iid`='&quot;.$row&#x5B;'top_iid'].&quot;';&quot;);\r\n            $row2 = $rs2-&gt;fetchArray();\r\n            if(isset($row2&#x5B;'iid']) === TRUE){\r\n              $row&#x5B;'title'] = $row2&#x5B;'title'];\r\n              $ret&#x5B;] = $row;\r\n            }\r\n          }\r\n        }\r\n      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n\r\n  \/\/ \u8a18\u4e8b\u3092\u53d6\u5f97\r\n  public function get_item( $tid, $iid=-1 ){\r\n    $ret = array();\r\n    if($this-&gt;m_db != NULL){\r\n      try {\r\n        $where = &quot;`tid`='$tid'&quot;;\r\n        if($iid != -1){\r\n          $where .= &quot; `iid`='$iid'&quot;;\r\n        }\r\n        \/\/ \u8a18\u4e8b\u60c5\u5831\u3092\u53d6\u5f97\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT * FROM `item` WHERE $where ORDER BY `iid`;&quot;);\r\n        while($row = $rs-&gt;fetchArray()){\r\n          if(isset($row&#x5B;'iid']) === TRUE){\r\n            $ret&#x5B;] = $row;\r\n          }\r\n        }\r\n      }catch(Exception $e){\r\n        echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n      }\r\n    }\r\n    return $ret;\r\n  }\r\n}\r\n?&gt;\r\n<\/pre>\n<hr class=my_hr_bottom>\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 \u307e\u305a\u306f\u300c\u6295\u7a3f\u300d\u3001\u300c\u7de8\u96c6\u300d\u3001\u300c\u524a\u9664\u300d\u3092\u5b9f\u88c5\u3059\u308b\u3002 \u753b\u9762\u30ec\u30a4\u30a2\u30a6\u30c8\u306f index.php \u306b\u66f8\u304f\u3088\u3046\u306b\u5207\u308a\u96e2\u3057\u3066\u3042\u308b\u306e\u3067\u3001\u30c7\u30b6\u30a4\u30f3\u304d\u308c\u3044\u5316\u306f\u5f8c\u3067&#8230; \u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002 http:\/\/w\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog17\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"views":9461,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":27,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":1741,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/276\/revisions\/1741"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}