{"id":2401,"date":"2025-03-05T23:09:41","date_gmt":"2025-03-05T15:09:41","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2401"},"modified":"2025-03-05T23:09:41","modified_gmt":"2025-03-05T15:09:41","slug":"sqlite-%e4%b8%8e-php-%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/05\/sqlite-%e4%b8%8e-php-%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"SQLite \u4e0e PHP \u7684\u4f7f\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQLite \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5c0f\u578b\u5e94\u7528\u3001\u7f51\u7ad9\u6216\u79fb\u52a8\u7aef\u9879\u76ee\u3002PHP \u5185\u7f6e\u652f\u6301 SQLite\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 SQLite \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5b89\u88c5\u989d\u5916\u7684\u6570\u636e\u5e93\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u68c0\u67e5 PHP \u662f\u5426\u652f\u6301 SQLite<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>phpinfo()<\/code> \u68c0\u67e5\u662f\u5426\u5df2\u542f\u7528 SQLite\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nphpinfo();\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u770b\u5230 <strong>SQLite3<\/strong> \u6216 <strong>PDO_SQLITE<\/strong>\uff0c\u8bf4\u660e\u5df2\u7ecf\u542f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684 PHP \u73af\u5883\u672a\u542f\u7528 SQLite\uff0c\u53ef\u4ee5\u5728 <code>php.ini<\/code> \u6587\u4ef6\u4e2d\u53d6\u6d88\u4ee5\u4e0b\u884c\u7684\u6ce8\u91ca\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension=sqlite3\nextension=pdo_sqlite<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u91cd\u65b0\u542f\u52a8 Web \u670d\u52a1\u5668\uff08Apache \u6216 Nginx\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 SQLite3 \u6269\u5c55<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1. \u521b\u5efa SQLite \u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PHP \u63d0\u4f9b <code>SQLite3<\/code> \u7c7b\uff0c\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c SQLite \u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db'); \/\/ \u521b\u5efa\u6216\u6253\u5f00 SQLite \u6570\u636e\u5e93\necho \"\u6570\u636e\u5e93\u8fde\u63a5\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u540e\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210 <code>test.db<\/code> \u6587\u4ef6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2. \u521b\u5efa\u8868<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db');\n\n$query = \"CREATE TABLE IF NOT EXISTS users (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    name TEXT NOT NULL,\n    age INTEGER NOT NULL\n)\";\n$db-&gt;exec($query);\necho \"\u8868\u5df2\u521b\u5efa\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3. \u63d2\u5165\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db');\n\n$stmt = $db-&gt;prepare(\"INSERT INTO users (name, age) VALUES (:name, :age)\");\n$stmt-&gt;bindValue(':name', 'Alice', SQLITE3_TEXT);\n$stmt-&gt;bindValue(':age', 25, SQLITE3_INTEGER);\n$stmt-&gt;execute();\n\necho \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4. \u67e5\u8be2\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db');\n\n$result = $db-&gt;query(\"SELECT * FROM users\");\nwhile ($row = $result-&gt;fetchArray(SQLITE3_ASSOC)) {\n    echo \"ID: \" . $row&#91;'id'] . \" - \u59d3\u540d: \" . $row&#91;'name'] . \" - \u5e74\u9f84: \" . $row&#91;'age'] . \"&lt;br&gt;\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5. \u66f4\u65b0\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db');\n\n$stmt = $db-&gt;prepare(\"UPDATE users SET age = :age WHERE name = :name\");\n$stmt-&gt;bindValue(':age', 30, SQLITE3_INTEGER);\n$stmt-&gt;bindValue(':name', 'Alice', SQLITE3_TEXT);\n$stmt-&gt;execute();\n\necho \"\u6570\u636e\u66f4\u65b0\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.6. \u5220\u9664\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$db = new SQLite3('test.db');\n\n$stmt = $db-&gt;prepare(\"DELETE FROM users WHERE name = :name\");\n$stmt-&gt;bindValue(':name', 'Alice', SQLITE3_TEXT);\n$stmt-&gt;execute();\n\necho \"\u6570\u636e\u5220\u9664\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 PDO \u8fde\u63a5 SQLite<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PDO\uff08PHP Data Objects\uff09\u662f PHP \u63d0\u4f9b\u7684\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u652f\u6301 SQLite\u3001MySQL\u3001PostgreSQL \u7b49\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1. \u8fde\u63a5 SQLite \u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    $pdo = new PDO(\"sqlite:test.db\");\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    echo \"\u6210\u529f\u8fde\u63a5 SQLite \u6570\u636e\u5e93\";\n} catch (PDOException $e) {\n    echo \"\u8fde\u63a5\u5931\u8d25: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2. \u521b\u5efa\u8868<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n\n$sql = \"CREATE TABLE IF NOT EXISTS users (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    name TEXT NOT NULL,\n    age INTEGER NOT NULL\n)\";\n$pdo-&gt;exec($sql);\n\necho \"\u8868\u521b\u5efa\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3. \u63d2\u5165\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n\n$stmt = $pdo-&gt;prepare(\"INSERT INTO users (name, age) VALUES (?, ?)\");\n$stmt-&gt;execute(&#91;'Bob', 28]);\n\necho \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4. \u67e5\u8be2\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n\n$stmt = $pdo-&gt;query(\"SELECT * FROM users\");\nwhile ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n    echo \"ID: \" . $row&#91;'id'] . \" - \u59d3\u540d: \" . $row&#91;'name'] . \" - \u5e74\u9f84: \" . $row&#91;'age'] . \"&lt;br&gt;\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.5. \u66f4\u65b0\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n\n$stmt = $pdo-&gt;prepare(\"UPDATE users SET age = ? WHERE name = ?\");\n$stmt-&gt;execute(&#91;35, 'Bob']);\n\necho \"\u6570\u636e\u66f4\u65b0\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.6. \u5220\u9664\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n\n$stmt = $pdo-&gt;prepare(\"DELETE FROM users WHERE name = ?\");\n$stmt-&gt;execute(&#91;'Bob']);\n\necho \"\u6570\u636e\u5220\u9664\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. SQLite \u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1. \u4e8b\u52a1\uff08\u63d0\u9ad8\u6027\u80fd\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n$pdo-&gt;beginTransaction();\n\ntry {\n    $pdo-&gt;exec(\"INSERT INTO users (name, age) VALUES ('Eve', 22)\");\n    $pdo-&gt;exec(\"INSERT INTO users (name, age) VALUES ('David', 30)\");\n    $pdo-&gt;commit();\n    echo \"\u4e8b\u52a1\u63d0\u4ea4\u6210\u529f\";\n} catch (Exception $e) {\n    $pdo-&gt;rollBack();\n    echo \"\u4e8b\u52a1\u56de\u6eda\uff1a\" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2. \u4f7f\u7528\u7d22\u5f15\u4f18\u5316\u67e5\u8be2<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n$pdo-&gt;exec(\"CREATE INDEX idx_name ON users(name)\");\necho \"\u7d22\u5f15\u521b\u5efa\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3. \u542f\u7528 WAL \u6a21\u5f0f\uff08\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:test.db\");\n$pdo-&gt;exec(\"PRAGMA journal_mode=WAL;\");\necho \"WAL \u6a21\u5f0f\u542f\u7528\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.4. \u8bbe\u7f6e\u53ea\u8bfb\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"sqlite:file:test.db?mode=ro\");\necho \"\u6570\u636e\u5e93\u53ea\u8bfb\u6a21\u5f0f\";\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4e3b\u9898<\/th><th>SQLite3<\/th><th>PDO<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/td><td><code>$db = new SQLite3('test.db');<\/code><\/td><td><code>$pdo = new PDO(\"sqlite:test.db\");<\/code><\/td><\/tr><tr><td><strong>\u521b\u5efa\u8868<\/strong><\/td><td><code>$db-&gt;exec(\"CREATE TABLE ...\")<\/code><\/td><td><code>$pdo-&gt;exec(\"CREATE TABLE ...\")<\/code><\/td><\/tr><tr><td><strong>\u63d2\u5165\u6570\u636e<\/strong><\/td><td><code>$stmt-&gt;bindValue(':name', 'Alice', SQLITE3_TEXT);<\/code><\/td><td><code>$stmt-&gt;execute(['Alice', 25]);<\/code><\/td><\/tr><tr><td><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/td><td><code>$result-&gt;fetchArray(SQLITE3_ASSOC);<\/code><\/td><td><code>$stmt-&gt;fetch(PDO::FETCH_ASSOC);<\/code><\/td><\/tr><tr><td><strong>\u4e8b\u52a1<\/strong><\/td><td><code>$db-&gt;exec(\"BEGIN TRANSACTION\");<\/code><\/td><td><code>$pdo-&gt;beginTransaction();<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u9002\u5408\u5c0f\u578b Web \u5e94\u7528\uff0c\u5982\u679c\u4f60\u7684 PHP \u9879\u76ee\u4e0d\u9700\u8981\u9ad8\u5e76\u53d1\u3001\u5206\u5e03\u5f0f\u6570\u636e\u5e93\uff0c\u90a3\u4e48 SQLite \u662f\u4e00\u4e2a\u7b80\u5355\u3001\u9ad8\u6548\u7684\u9009\u62e9\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLite \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5c0f\u578b\u5e94\u7528\u3001\u7f51\u7ad9\u6216\u79fb\u52a8\u7aef\u9879\u76ee\u3002PHP \u5185\u7f6e\u652f\u6301 SQLite [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2401","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2401"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2401\/revisions"}],"predecessor-version":[{"id":2402,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2401\/revisions\/2402"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}