{"id":2403,"date":"2025-03-05T23:14:24","date_gmt":"2025-03-05T15:14:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2403"},"modified":"2025-03-05T23:14:24","modified_gmt":"2025-03-05T15:14:24","slug":"sqlite-%e4%b8%8e-perl-%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-perl-%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"SQLite \u4e0e Perl \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\u9002\u7528\u4e8e\u5c0f\u578b\u5e94\u7528\u3001\u811a\u672c\u548c\u6d4b\u8bd5\u73af\u5883\u3002Perl \u63d0\u4f9b\u4e86 <code>DBI<\/code>\uff08\u6570\u636e\u5e93\u63a5\u53e3\u6a21\u5757\uff09\u548c <code>DBD::SQLite<\/code>\uff08SQLite \u9a71\u52a8\uff09\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c SQLite \u6570\u636e\u5e93\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. \u5b89\u88c5 SQLite \u6a21\u5757<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 SQLite \u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5 <code>DBI<\/code> \u548c <code>DBD::SQLite<\/code> \u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cpan DBI\ncpan DBD::SQLite<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\u4f7f\u7528 <code>cpanm<\/code>\uff08\u63a8\u8350\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cpanm DBI\ncpanm DBD::SQLite<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f Debian\/Ubuntu\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install libdbd-sqlite3-perl<\/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>2. \u8fde\u63a5 SQLite \u6570\u636e\u5e93<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>DBI-&gt;connect<\/code> \u8fde\u63a5 SQLite \u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use strict;\nuse warnings;\nuse DBI;\n\nmy $db_file = \"test.db\";\nmy $dbh = DBI-&gt;connect(\"dbi:SQLite:dbname=$db_file\",\"\",\"\", { RaiseError =&gt; 1, AutoCommit =&gt; 1 })\n    or die $DBI::errstr;\n\nprint \"\u6210\u529f\u8fde\u63a5 SQLite \u6570\u636e\u5e93\\n\";<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>test.db<\/code> \u6587\u4ef6\u4e0d\u5b58\u5728\uff0cSQLite \u4f1a\u81ea\u52a8\u521b\u5efa\u5b83\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>3. \u521b\u5efa\u8868<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>my $sql = qq{\n    CREATE TABLE IF NOT EXISTS users (\n        id INTEGER PRIMARY KEY AUTOINCREMENT,\n        name TEXT NOT NULL,\n        age INTEGER NOT NULL\n    );\n};\n$dbh-&gt;do($sql);\nprint \"\u8868\u521b\u5efa\u6210\u529f\\n\";<\/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. \u63d2\u5165\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>my $sth = $dbh-&gt;prepare(\"INSERT INTO users (name, age) VALUES (?, ?)\");\n$sth-&gt;execute(\"Alice\", 25);\n$sth-&gt;execute(\"Bob\", 30);\n$sth-&gt;finish();\nprint \"\u6570\u636e\u63d2\u5165\u6210\u529f\\n\";<\/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>5. \u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>my $sth = $dbh-&gt;prepare(\"SELECT * FROM users\");\n$sth-&gt;execute();\n\nwhile (my @row = $sth-&gt;fetchrow_array) {\n    print \"ID: $row&#91;0] - \u59d3\u540d: $row&#91;1] - \u5e74\u9f84: $row&#91;2]\\n\";\n}\n\n$sth-&gt;finish();<\/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>6. \u66f4\u65b0\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>my $sth = $dbh-&gt;prepare(\"UPDATE users SET age = ? WHERE name = ?\");\n$sth-&gt;execute(28, \"Alice\");\nprint \"\u6570\u636e\u66f4\u65b0\u6210\u529f\\n\";<\/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>7. \u5220\u9664\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>my $sth = $dbh-&gt;prepare(\"DELETE FROM users WHERE name = ?\");\n$sth-&gt;execute(\"Bob\");\nprint \"\u6570\u636e\u5220\u9664\u6210\u529f\\n\";<\/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>8. \u4f7f\u7528\u4e8b\u52a1<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>eval {\n    $dbh-&gt;begin_work;\n    $dbh-&gt;do(\"INSERT INTO users (name, age) VALUES ('Eve', 22)\");\n    $dbh-&gt;do(\"INSERT INTO users (name, age) VALUES ('David', 35)\");\n    $dbh-&gt;commit;\n    print \"\u4e8b\u52a1\u63d0\u4ea4\u6210\u529f\\n\";\n};\nif ($@) {\n    $dbh-&gt;rollback;\n    print \"\u4e8b\u52a1\u56de\u6eda: $@\\n\";\n}<\/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>9. \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$dbh-&gt;disconnect();\nprint \"\u6570\u636e\u5e93\u8fde\u63a5\u5df2\u5173\u95ed\\n\";<\/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>10. \u5176\u4ed6 SQLite \u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10.1. \u521b\u5efa\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$dbh-&gt;do(\"CREATE INDEX idx_name ON users(name)\");\nprint \"\u7d22\u5f15\u521b\u5efa\u6210\u529f\\n\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10.2. \u542f\u7528 WAL \u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$dbh-&gt;do(\"PRAGMA journal_mode=WAL;\");\nprint \"WAL \u6a21\u5f0f\u542f\u7528\\n\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10.3. \u53ea\u8bfb\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>my $dbh_ro = DBI-&gt;connect(\"dbi:SQLite:dbname=test.db\",\"\",\"\", { ReadOnly =&gt; 1 });\nprint \"\u6570\u636e\u5e93\u53ea\u8bfb\u6a21\u5f0f\\n\";<\/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>\u64cd\u4f5c<\/th><th>SQL \u8bed\u53e5<\/th><th>Perl \u4ee3\u7801<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/td><td><code>dbi:SQLite:dbname=test.db<\/code><\/td><td><code>DBI-&gt;connect(...)<\/code><\/td><\/tr><tr><td><strong>\u521b\u5efa\u8868<\/strong><\/td><td><code>CREATE TABLE ...<\/code><\/td><td><code>$dbh-&gt;do(...)<\/code><\/td><\/tr><tr><td><strong>\u63d2\u5165\u6570\u636e<\/strong><\/td><td><code>INSERT INTO users (name, age) VALUES (?, ?)<\/code><\/td><td><code>$sth-&gt;execute(...)<\/code><\/td><\/tr><tr><td><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/td><td><code>SELECT * FROM users<\/code><\/td><td><code>$sth-&gt;fetchrow_array<\/code><\/td><\/tr><tr><td><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/td><td><code>UPDATE users SET age = ? WHERE name = ?<\/code><\/td><td><code>$sth-&gt;execute(...)<\/code><\/td><\/tr><tr><td><strong>\u5220\u9664\u6570\u636e<\/strong><\/td><td><code>DELETE FROM users WHERE name = ?<\/code><\/td><td><code>$sth-&gt;execute(...)<\/code><\/td><\/tr><tr><td><strong>\u4e8b\u52a1<\/strong><\/td><td><code>BEGIN TRANSACTION<\/code><\/td><td><code>$dbh-&gt;begin_work<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u9002\u7528\u4e8e\u5c0f\u578b\u9879\u76ee\uff0c\u800c Perl \u901a\u8fc7 <code>DBI<\/code> \u8ba9\u64cd\u4f5c SQLite \u53d8\u5f97\u7b80\u5355\u76f4\u89c2\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u5f3a\u7684\u6570\u636e\u5e93\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 MySQL \u6216 PostgreSQL\u3002<\/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\u9002\u7528\u4e8e\u5c0f\u578b\u5e94\u7528\u3001\u811a\u672c\u548c\u6d4b\u8bd5\u73af\u5883\u3002Perl \u63d0\u4f9b\u4e86 DBI\uff08\u6570\u636e\u5e93\u63a5\u53e3 [&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-2403","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2403","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=2403"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2403\/revisions"}],"predecessor-version":[{"id":2404,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2403\/revisions\/2404"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}