{"id":2372,"date":"2025-03-04T20:37:39","date_gmt":"2025-03-04T12:37:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2372"},"modified":"2025-03-04T20:37:39","modified_gmt":"2025-03-04T12:37:39","slug":"sqlite-%e4%ba%8b%e5%8a%a1%ef%bc%88transaction%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/04\/sqlite-%e4%ba%8b%e5%8a%a1%ef%bc%88transaction%ef%bc%89\/","title":{"rendered":"SQLite\u00a0\u4e8b\u52a1\uff08Transaction\uff09"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 SQLite \u4e2d\uff0c<strong>\u4e8b\u52a1\uff08Transaction\uff09<\/strong> \u662f\u4e00\u7ec4\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u56de\u6eda\u3002\u4e8b\u52a1\u7528\u4e8e\u4fdd\u8bc1\u6570\u636e\u5e93\u64cd\u4f5c\u7684<strong>\u539f\u5b50\u6027\uff08Atomicity\uff09<\/strong>\u3001<strong>\u4e00\u81f4\u6027\uff08Consistency\uff09<\/strong>\u3001<strong>\u9694\u79bb\u6027\uff08Isolation\uff09<\/strong>\u548c<strong>\u6301\u4e45\u6027\uff08Durability\uff09<\/strong>\uff0c\u901a\u5e38\u88ab\u79f0\u4e3a <strong>ACID<\/strong> \u7279\u6027\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4e8b\u52a1\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u5b50\u6027<\/strong>\uff1a\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u8981\u4e48\u90fd\u6210\u529f\u6267\u884c\uff0c\u8981\u4e48\u90fd\u4e0d\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u81f4\u6027<\/strong>\uff1a\u4e8b\u52a1\u6267\u884c\u524d\u540e\uff0c\u6570\u636e\u5e93\u7684\u6570\u636e\u59cb\u7ec8\u4fdd\u6301\u4e00\u81f4\u6027\u3002<\/li>\n\n\n\n<li><strong>\u9694\u79bb\u6027<\/strong>\uff1a\u4e8b\u52a1\u4e4b\u95f4\u7684\u6267\u884c\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\uff0c\u4e00\u4e2a\u4e8b\u52a1\u7684\u6267\u884c\u4e0d\u4f1a\u5e72\u6270\u5176\u4ed6\u4e8b\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u6301\u4e45\u6027<\/strong>\uff1a\u4e00\u65e6\u4e8b\u52a1\u6210\u529f\u6267\u884c\uff0c\u5b83\u5bf9\u6570\u636e\u5e93\u7684\u6539\u53d8\u662f\u6c38\u4e45\u6027\u7684\uff0c\u5373\u4f7f\u53d1\u751f\u7cfb\u7edf\u5d29\u6e83\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. SQLite \u4e8b\u52a1\u547d\u4ee4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u4f7f\u7528 <code>BEGIN<\/code>, <code>COMMIT<\/code> \u548c <code>ROLLBACK<\/code> \u547d\u4ee4\u6765\u7ba1\u7406\u4e8b\u52a1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u5f00\u59cb\u4e8b\u52a1\uff08<code>BEGIN<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>BEGIN TRANSACTION<\/code> \u7528\u4e8e\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1\u3002\u4f60\u53ef\u4ee5\u5728\u4e8b\u52a1\u5185\u6267\u884c\u591a\u4e2a SQL \u64cd\u4f5c\uff0c\u76f4\u5230\u4f60\u51b3\u5b9a\u63d0\u4ea4\u6216\u56de\u6eda\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;\nINSERT INTO employees (id, first_name, last_name) VALUES (1, 'John', 'Doe');\nUPDATE employees SET last_name = 'Smith' WHERE id = 1;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u63d0\u4ea4\u4e8b\u52a1\uff08<code>COMMIT<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COMMIT<\/code> \u7528\u4e8e\u63d0\u4ea4\u5f53\u524d\u4e8b\u52a1\uff0c\u8fd9\u610f\u5473\u7740\u6240\u6709\u5728\u8be5\u4e8b\u52a1\u5185\u6267\u884c\u7684\u64cd\u4f5c\u5c06\u6c38\u4e45\u751f\u6548\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>COMMIT;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>COMMIT;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0c\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u4fee\u6539\u5c06\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 \u56de\u6eda\u4e8b\u52a1\uff08<code>ROLLBACK<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ROLLBACK<\/code> \u7528\u4e8e\u64a4\u9500\u5f53\u524d\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u3002\u5982\u679c\u5728\u4e8b\u52a1\u4e2d\u53d1\u751f\u9519\u8bef\uff0c\u6216\u8005\u4f60\u51b3\u5b9a\u4e0d\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u56de\u6eda\u4e8b\u52a1\u4ee5\u64a4\u9500\u4e4b\u524d\u7684\u66f4\u6539\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ROLLBACK;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ROLLBACK;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0c\u6240\u6709\u5728\u8be5\u4e8b\u52a1\u4e2d\u7684\u66f4\u6539\u5c06\u88ab\u64a4\u9500\uff0c\u6570\u636e\u5e93\u56de\u5230\u4e8b\u52a1\u5f00\u59cb\u4e4b\u524d\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u81ea\u52a8\u63d0\u4ea4\u6a21\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u9ed8\u8ba4\u542f\u7528\u81ea\u52a8\u63d0\u4ea4\u6a21\u5f0f\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u6bcf\u4e2a\u72ec\u7acb\u7684 SQL \u8bed\u53e5\u90fd\u4f1a\u88ab\u89c6\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8b\u52a1\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u6ca1\u6709\u663e\u5f0f\u5730\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1\uff0cSQLite \u4f1a\u5728\u6bcf\u4e2a\u8bed\u53e5\u6267\u884c\u540e\u81ea\u52a8\u63d0\u4ea4\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4e24\u4e2a\u64cd\u4f5c\u5c06\u5206\u522b\u88ab\u89c6\u4e3a\u72ec\u7acb\u7684\u4e8b\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (id, first_name, last_name) VALUES (1, 'John', 'Doe');\nUPDATE employees SET last_name = 'Smith' WHERE id = 1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u663e\u5f0f\u63a7\u5236\u4e8b\u52a1\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>BEGIN TRANSACTION<\/code> \u548c <code>COMMIT<\/code> \u6216 <code>ROLLBACK<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u4e8b\u52a1\u793a\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4e8b\u52a1\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong> \u4f7f\u7528\u4e8b\u52a1\u63d2\u5165\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;\n\nINSERT INTO employees (id, first_name, last_name) VALUES (1, 'John', 'Doe');\nINSERT INTO employees (id, first_name, last_name) VALUES (2, 'Jane', 'Doe');\n\nCOMMIT;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a <code>INSERT<\/code> \u64cd\u4f5c\u90fd\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u6267\u884c\uff0c\u5982\u679c\u6ca1\u6709\u95ee\u9898\uff0c<code>COMMIT<\/code> \u4f1a\u63d0\u4ea4\u66f4\u6539\u3002\u5982\u679c\u5728 <code>INSERT<\/code> \u8fc7\u7a0b\u4e2d\u53d1\u751f\u4efb\u4f55\u9519\u8bef\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>ROLLBACK<\/code> \u6765\u64a4\u9500\u6240\u6709\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRANSACTION;\n\nINSERT INTO employees (id, first_name, last_name) VALUES (1, 'John', 'Doe');\nINSERT INTO employees (id, first_name, last_name) VALUES (2, 'Jane', 'Doe');\n\n-- \u5047\u8bbe\u7b2c\u4e8c\u4e2a INSERT \u5931\u8d25\nROLLBACK;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ROLLBACK<\/code> \u4f1a\u64a4\u9500\u6240\u6709\u63d2\u5165\u64cd\u4f5c\uff0c\u6570\u636e\u5e93\u6062\u590d\u5230\u4e8b\u52a1\u5f00\u59cb\u4e4b\u524d\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4e8b\u52a1\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8b\u52a1\u901a\u5e38\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u4e2a\u6b65\u9aa4\u64cd\u4f5c<\/strong>\uff1a\u5f53\u4e00\u7ec4\u64cd\u4f5c\u5fc5\u987b\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u8fdb\u884c\u65f6\uff08\u4f8b\u5982\uff0c\u8f6c\u8d26\u64cd\u4f5c\uff1a\u4ece\u4e00\u4e2a\u8d26\u6237\u4e2d\u6263\u6b3e\u5e76\u5411\u53e6\u4e00\u4e2a\u8d26\u6237\u5b58\u6b3e\uff09\u3002<\/li>\n\n\n\n<li><strong>\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027<\/strong>\uff1a\u786e\u4fdd\u5728\u4e00\u7ec4\u76f8\u5173\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u5982\u679c\u6709\u4efb\u4f55\u4e00\u9879\u5931\u8d25\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u4e0d\u4f1a\u5bf9\u6570\u636e\u5e93\u4ea7\u751f\u5f71\u54cd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u652f\u6301\u51e0\u79cd\u9694\u79bb\u7ea7\u522b\uff0c\u63a7\u5236\u4e8b\u52a1\u4e4b\u95f4\u7684\u5e76\u53d1\u884c\u4e3a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>DEFERRED<\/code>\uff08\u9ed8\u8ba4\uff09<\/strong>\uff1a\u4e8b\u52a1\u4f1a\u63a8\u8fdf\u9501\u5b9a\uff0c\u76f4\u5230\u5b83\u9700\u8981\u8fdb\u884c\u5199\u64cd\u4f5c\u4e3a\u6b62\u3002\u5728\u4e8b\u52a1\u5f00\u59cb\u65f6\uff0cSQLite \u4e0d\u4f1a\u7acb\u5373\u9501\u5b9a\u6570\u636e\u5e93\uff0c\u76f4\u5230\u6267\u884c\u4fee\u6539\u64cd\u4f5c\u65f6\u624d\u4f1a\u9501\u5b9a\u3002<\/li>\n\n\n\n<li><strong><code>IMMEDIATE<\/code><\/strong>\uff1a\u5f53\u4e8b\u52a1\u5f00\u59cb\u65f6\uff0cSQLite \u4f1a\u7acb\u5373\u83b7\u53d6\u5199\u9501\uff0c\u76f4\u5230\u4e8b\u52a1\u5b8c\u6210\u4e3a\u6b62\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5176\u4ed6\u4e8b\u52a1\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong><code>EXCLUSIVE<\/code><\/strong>\uff1a\u5f53\u4e8b\u52a1\u5f00\u59cb\u65f6\uff0cSQLite \u4f1a\u7acb\u5373\u83b7\u53d6\u6570\u636e\u5e93\u7684\u72ec\u5360\u9501\uff0c\u5176\u4ed6\u4e8b\u52a1\u5c06\u65e0\u6cd5\u6267\u884c\u4efb\u4f55\u5199\u64cd\u4f5c\uff0c\u76f4\u5230\u5f53\u524d\u4e8b\u52a1\u5b8c\u6210\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN DEFERRED TRANSACTION;\n-- \u6267\u884c\u4e00\u4e9b\u67e5\u8be2\u6216\u66f4\u65b0\u64cd\u4f5c\nCOMMIT;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u52a1\u547d\u4ee4<\/strong>\uff1a<code>BEGIN TRANSACTION<\/code>, <code>COMMIT<\/code>, <code>ROLLBACK<\/code> \u7528\u4e8e\u63a7\u5236\u4e8b\u52a1\u7684\u5f00\u59cb\u3001\u63d0\u4ea4\u548c\u56de\u6eda\u3002<\/li>\n\n\n\n<li><strong>ACID \u7279\u6027<\/strong>\uff1a\u4e8b\u52a1\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u548c\u6301\u4e45\u6027\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u63d0\u4ea4<\/strong>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSQLite \u662f\u81ea\u52a8\u63d0\u4ea4\u6a21\u5f0f\uff0c\u6267\u884c\u6bcf\u4e2a SQL \u8bed\u53e5\u4f1a\u9690\u5f0f\u63d0\u4ea4\u4e8b\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u9694\u79bb\u7ea7\u522b<\/strong>\uff1aSQLite \u63d0\u4f9b\u4e86\u4e09\u79cd\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff1a<code>DEFERRED<\/code>\uff08\u9ed8\u8ba4\uff09\u3001<code>IMMEDIATE<\/code> \u548c <code>EXCLUSIVE<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8b\u52a1\u5728\u6570\u636e\u5e93\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u6d89\u53ca\u591a\u4e2a\u64cd\u4f5c\u9700\u8981\u786e\u4fdd\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u4e8b\u52a1\u53ef\u4ee5\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u4e2d\u65ad\u64cd\u4f5c\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQLite \u4e2d\uff0c\u4e8b\u52a1\uff08Transaction\uff09 \u662f\u4e00\u7ec4\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u56de\u6eda [&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-2372","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2372","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=2372"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2372\/revisions"}],"predecessor-version":[{"id":2373,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2372\/revisions\/2373"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}