{"id":3973,"date":"2023-12-22T22:05:15","date_gmt":"2023-12-22T14:05:15","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3973"},"modified":"2023-12-22T22:05:17","modified_gmt":"2023-12-22T14:05:17","slug":"php%e6%93%8d%e4%bd%9csqlite3%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=3973","title":{"rendered":"php\u64cd\u4f5csqlite3\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/\/ \u521b\u5efa\u4e00\u4e2a SQLite3 \u7c7b\u7684\u5bf9\u8c61\r\nclass MyDB extends SQLite3 {\r\n  function __construct() {\r\n    \/\/ \u6253\u5f00\u6216\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u6587\u4ef6\r\n    $this->open('test.db');\r\n  }\r\n}\r\n\r\n\/\/ \u5b9e\u4f8b\u5316\u5bf9\u8c61\r\n$db = new MyDB();\r\n\r\n\/\/ \u68c0\u67e5\u662f\u5426\u6210\u529f\u6253\u5f00\u6570\u636e\u5e93\r\nif(!$db) {\r\n  echo $db->lastErrorMsg();\r\n} else {\r\n  echo \"Opened database successfully\\n\";\r\n}\r\n\r\n\/\/ \u521b\u5efa\u4e00\u4e2a SQL \u8bed\u53e5\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u8868\r\n$sql =&lt;&lt;&lt;EOF\r\n  CREATE TABLE COMPANY (\r\n  ID INT PRIMARY KEY NOT NULL,\r\n  NAME TEXT NOT NULL,\r\n  AGE INT NOT NULL,\r\n  ADDRESS CHAR(50),\r\n  SALARY REAL\r\n  );\r\nEOF;\r\n\r\n\/\/ \u6267\u884c SQL \u8bed\u53e5\uff0c\u65e0\u8fd4\u56de\u7ed3\u679c\r\n$ret = $db->exec($sql);\r\n\r\n\/\/ \u68c0\u67e5\u662f\u5426\u6210\u529f\u6267\u884c\r\nif(!$ret) {\r\n  echo $db->lastErrorMsg();\r\n} else {\r\n  echo \"Table created successfully\\n\";\r\n}\r\n\r\n\/\/ \u521b\u5efa\u4e00\u4e2a SQL \u8bed\u53e5\uff0c\u7528\u4e8e\u63d2\u5165\u4e00\u4e9b\u6570\u636e\r\n$sql =&lt;&lt;&lt;EOF\r\n  INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\r\n  VALUES (1, 'Paul', 32, 'California', 20000.00 );\r\n\r\n  INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\r\n  VALUES (2, 'Allen', 25, 'Texas', 15000.00 );\r\n\r\n  INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\r\n  VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );\r\n\r\n  INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\r\n  VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );\r\nEOF;\r\n\r\n\/\/ \u6267\u884c SQL \u8bed\u53e5\uff0c\u65e0\u8fd4\u56de\u7ed3\u679c\r\n$ret = $db->exec($sql);\r\n\r\n\/\/ \u68c0\u67e5\u662f\u5426\u6210\u529f\u6267\u884c\r\nif(!$ret) {\r\n  echo $db->lastErrorMsg();\r\n} else {\r\n  echo \"Records created successfully\\n\";\r\n}\r\n\r\n\/\/ \u521b\u5efa\u4e00\u4e2a SQL \u8bed\u53e5\uff0c\u7528\u4e8e\u67e5\u8be2\u6570\u636e\r\n$sql =&lt;&lt;&lt;EOF\r\n  SELECT * from COMPANY;\r\nEOF;\r\n\r\n\/\/ \u6267\u884c SQL \u8bed\u53e5\uff0c\u6709\u8fd4\u56de\u7ed3\u679c\r\n$ret = $db->query($sql);\r\n\r\n\/\/ \u904d\u5386\u7ed3\u679c\u96c6\uff0c\u83b7\u53d6\u6bcf\u4e00\u884c\u6570\u636e\r\nwhile($row = $ret->fetchArray(SQLITE3_ASSOC) ) {\r\n  echo \"ID = \". $row&#91;'ID'] . \"\\n\";\r\n  echo \"NAME = \". $row&#91;'NAME'] .\"\\n\";\r\n  echo \"ADDRESS = \". $row&#91;'ADDRESS'] .\"\\n\";\r\n  echo \"SALARY = \".$row&#91;'SALARY'] .\"\\n\\n\";\r\n}\r\n\r\n\/\/ \u663e\u793a\u64cd\u4f5c\u6210\u529f\u7684\u4fe1\u606f\r\necho \"Operation done successfully\\n\";\r\n\r\n\/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\n$db->close();\r\n?>\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3973","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3973"}],"version-history":[{"count":1,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3973\/revisions"}],"predecessor-version":[{"id":3974,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3973\/revisions\/3974"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3973"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}