{"id":81,"date":"2016-10-22T21:33:34","date_gmt":"2016-10-22T12:33:34","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=81"},"modified":"2025-04-26T19:15:01","modified_gmt":"2025-04-26T10:15:01","slug":"blog6","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog6\/","title":{"rendered":"(6) SQLite\u3067\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u4f5c\u6210 #1"},"content":{"rendered":"<h1 class=my_h>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068<\/h1>\n<p>\u7c21\u6613\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u3057\u3066SQLite\u306f\u3068\u3063\u3066\u3082\u4fbf\u5229\u3002<br \/>\n\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306a\u306e\u3067\u3001\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u6642\u306e\u6392\u4ed6\u5236\u5fa1\u3092\u3084\u3063\u3066\u304f\u308c\u308b\u3002<br \/>\n\u3088\u3063\u3066SQLite\u3092\u4f7f\u3046\u3068\u5bb9\u6613\u306b\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3002<\/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\/00006\/\" target=\"_blank\">http:\/\/www.dogrow.net\/php\/sample\/00006\/<\/a><\/p>\n<h1 class=my_h>\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210<\/h1>\n<p>\u30ab\u30a6\u30f3\u30bf\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5225\u30d5\u30a1\u30a4\u30eb\u3067\u30af\u30e9\u30b9\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002<\/p>\n<pre class=\"brush: php; title: index.php; notranslate\" title=\"index.php\">\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\r\n&lt;?php\r\n  require_once(&quot;CCounter.php&quot;);\r\n  $cCnt = new CCounter();\r\n  $cCnt-&gt;connect_db();        \/\/ DB\u63a5\u7d9a\r\n  $cnt = $cCnt-&gt;count_up();   \/\/ \u30ab\u30a6\u30f3\u30c8\u30a2\u30c3\u30d7\uff06\u5024\u53d6\u5f97\r\n  $cCnt-&gt;disconnect_db();     \/\/ DB\u5207\u65ad\r\n  echo &quot;counter : &quot;.$cnt;     \/\/ \u30ab\u30a6\u30f3\u30c8\u5024\u3092\u8868\u793a\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<pre class=\"brush: php; title: CCounter.php; notranslate\" title=\"CCounter.php\">\r\n&lt;?php\r\nclass CCounter {\r\n  private $m_db;        \/\/ SQLite3 instance\r\n  private $m_db_fpath;  \/\/ DB\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\r\n  function __construct( $fpath = &quot;cnt.db&quot; ) {\r\n    $this-&gt;m_db = null;\r\n    $this-&gt;m_db_fpath = $fpath;\r\n  }\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ DB\u63a5\u7d9a\r\n  function connect_db(){\r\n    if($this-&gt;m_db == null){\r\n      \/\/ DB\u30d5\u30a1\u30a4\u30eb\u5b58\u5728\u78ba\u8a8d\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      \/\/ DB\u306f\u65b0\u898f\u4f5c\u6210\uff1f\r\n      if($isDbExist === FALSE){\r\n        try {\r\n          \/\/ \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u3001\u30ab\u30a6\u30f3\u30c8\u5024\u30920\u3067\u521d\u671f\u5316\r\n          $this-&gt;m_db-&gt;exec(&quot;CREATE TABLE `mytbl` (`cnt` INTEGER);&quot;);\r\n          $this-&gt;m_db-&gt;exec(&quot;INSERT INTO `mytbl` (`cnt`) VALUES(0);&quot;);\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    }\r\n  }\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ DB\u5207\u65ad\r\n  function disconnect_db(){\r\n    if($this-&gt;m_db != null){\r\n      $this-&gt;m_db-&gt;close();\r\n      $this-&gt;m_db = null;\r\n    }\r\n  }\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/ \u30ab\u30a6\u30f3\u30bf\u66f4\u65b0\uff06\u66f4\u65b0\u5024\u3092\u53d6\u5f97\r\n  function count_up(){\r\n    $cnt = 0;\r\n    if($this-&gt;m_db != null){\r\n      try {\r\n        \/\/ \u30ab\u30a6\u30f3\u30bf\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\r\n        $this-&gt;m_db-&gt;exec(&quot;UPDATE `mytbl` SET `cnt`=`cnt`+1;&quot;);\r\n        \/\/ \u30ab\u30a6\u30f3\u30bf\u306e\u5024\u3092\u53d6\u5f97\r\n        $rs = $this-&gt;m_db-&gt;query(&quot;SELECT `cnt` FROM `mytbl`;&quot;);\r\n        $row = $rs-&gt;fetchArray();\r\n        $cnt = $row&#x5B;'cnt'];\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 $cnt;\r\n  }\r\n}\r\n?&gt;\r\n<\/pre>\n<p>\u3000<br \/>\n\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/www.dogrow.net\/php\/sample\/00006\/\" target=\"_blank\">http:\/\/www.dogrow.net\/php\/sample\/00006\/<\/a><\/p>\n<hr class=my_hr_bottom>\n","protected":false},"excerpt":{"rendered":"<p>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068 \u7c21\u6613\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u3057\u3066SQLite\u306f\u3068\u3063\u3066\u3082\u4fbf\u5229\u3002 \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306a\u306e\u3067\u3001\u30c7\u30fc\u30bf\u30a2\u30af\u30bb\u30b9\u6642\u306e\u6392\u4ed6\u5236\u5fa1\u3092\u3084\u3063\u3066\u304f\u308c\u308b\u3002 \u3088\u3063\u3066SQLite\u3092\u4f7f\u3046\u3068\u5bb9\u6613\u306b\u30a2\u30af\u30bb\u30b9\u30ab\u30a6\u30f3\u30bf\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3002 \u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog6\/\">\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,2],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-sqlite","category-2"],"views":2752,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/81","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=81"}],"version-history":[{"count":34,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":2121,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/81\/revisions\/2121"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}