{"id":123,"date":"2026-08-06T00:31:35","date_gmt":"2026-08-05T16:31:35","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=123"},"modified":"2026-08-06T00:46:26","modified_gmt":"2026-08-05T16:46:26","slug":"day-28-sqlite-%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%9a%e5%bb%ba%e8%a1%a8%e3%80%81crud%e3%80%81%e5%8f%82%e6%95%b0%e5%8c%96%e6%9f%a5%e8%af%a2%e4%b8%8e%e4%ba%8b%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-28-sqlite-%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%9a%e5%bb%ba%e8%a1%a8%e3%80%81crud%e3%80%81%e5%8f%82%e6%95%b0%e5%8c%96%e6%9f%a5%e8%af%a2%e4%b8%8e%e4%ba%8b%e5%8a%a1\/","title":{"rendered":"| Day 28 | SQLite \u6570\u636e\u5e93\uff1a\u5efa\u8868\u3001CRUD\u3001\u53c2\u6570\u5316\u67e5\u8be2\u4e0e\u4e8b\u52a1 |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u6570\u636e\u5e93\u4e0e\u8868 \u2014\u2014 \u5148\u60f3\u6e05\u695a\u300c\u5b58\u4ec0\u4e48\u300d\uff0c\u518d\u52a8\u624b\u5efa\u8868<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6570\u636e\u5e93<\/strong>\uff08database\uff09\u662f<strong>\u6709\u7ec4\u7ec7\u5730\u5b58\u653e\u6570\u636e\u7684\u4ed3\u5e93<\/strong>\uff0c<strong>\u8868<\/strong>\uff08table\uff09\u662f\u4ed3\u5e93\u91cc\u7684<strong>\u5206\u7c7b\u8d27\u67b6<\/strong>\u3002\u6587\u4ef6\u5b58\u6570\u636e\uff08CSV\/JSON\uff09\u662f\u300c\u628a\u4e1c\u897f\u5806\u5728\u684c\u4e0a\u300d\uff0c\u6570\u636e\u5e93\u662f\u300c\u628a\u4e1c\u897f\u5206\u95e8\u522b\u7c7b\u653e\u8fdb\u5e26\u6807\u7b7e\u7684\u683c\u5b50\u300d\u3002\u4e4b\u524d\u5b58\u6570\u636e\u7528\u6587\u4ef6\uff0c\u4f46\u6587\u4ef6\u6709\u4e2a\u95ee\u9898\u2014\u2014<strong>\u6570\u636e\u4e00\u591a\u5c31\u4e71<\/strong>\uff1a\u6ca1\u6cd5\u6309\u6761\u4ef6\u5feb\u901f\u67e5\u3001\u591a\u4eba\u540c\u65f6\u5199\u4f1a\u51b2\u7a81\u3001\u6570\u636e\u5173\u7cfb\u590d\u6742\u65f6\u65e0\u4ece\u4e0b\u624b\u3002\u6570\u636e\u5e93\u5c31\u662f\u4e13\u95e8\u5e72\u8fd9\u4e2a\u7684\uff1a<strong>\u6d77\u91cf\u3001\u6709\u5e8f\u3001\u80fd\u67e5\u3001\u80fd\u5e76\u53d1<\/strong>\u3002SQLite \u662f Python \u81ea\u5e26\u7684\u8f7b\u91cf\u6570\u636e\u5e93\uff0c\u4e0d\u7528\u88c5\u670d\u52a1\u5668\u3001\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u6700\u9002\u5408\u5165\u95e8\uff1b\u5b66\u4f1a\u5b83\uff0cSQL \u8bed\u6cd5\u548c\u6240\u6709\u4e3b\u6d41\u6570\u636e\u5e93\uff08MySQL\u3001PostgreSQL\uff09\u662f\u901a\u7528\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u628a\u6574\u4e2a\u6570\u636e\u5e93\u505a\u6210<strong>\u4e00\u4e2a\u6587\u4ef6<\/strong>\uff08<code>.db<\/code>\uff09\uff0c\u6587\u4ef6\u5185\u90e8\u6309\u300c\u9875\u300d\uff08page\uff0c\u901a\u5e38 4KB\uff09\u7ec4\u7ec7\uff0c\u8868\u7684\u6570\u636e\u4ee5 B \u6811\uff08\u4e00\u79cd\u80fd\u5feb\u901f\u67e5\u627e\u7684\u6811\u72b6\u7ed3\u6784\uff09\u5b58\u50a8\uff0c\u4e3b\u952e\u5c31\u662f\u6811\u7684\u300c\u7d22\u5f15\u952e\u300d\uff0c\u6240\u4ee5\u6309\u4e3b\u952e\u67e5\u6570\u636e\u5feb\u5230\u51e0\u4e4e\u4e0d\u968f\u6570\u636e\u91cf\u589e\u957f\u53d8\u6162\u3002SQLite \u662f<strong>\u5d4c\u5165\u5f0f\u6570\u636e\u5e93<\/strong>\uff1a\u6ca1\u6709\u72ec\u7acb\u670d\u52a1\u5668\u8fdb\u7a0b\uff0c\u4f60\u7684\u7a0b\u5e8f\u76f4\u63a5\u8bfb\u5199\u8fd9\u4e2a\u6587\u4ef6\uff08\u5bf9\u6bd4 MySQL \u8981\u88c5\u540e\u53f0\u670d\u52a1\uff09\u2014\u2014\u8fd9\u5c31\u662f\u5b83\u8f7b\u91cf\u3001\u9002\u5408\u5165\u95e8\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u5e93\u50cf\u4e00\u4e2a<strong>\u56fe\u4e66\u9986<\/strong>\uff1a\u56fe\u4e66\u9986\uff08\u6570\u636e\u5e93\uff09\u91cc\u6709\u4e00\u6392\u6392\u4e66\u67b6\uff08\u8868\uff09\uff0c\u6bcf\u672c\u4e66\uff08\u8bb0\u5f55\uff09\u6709\u56fa\u5b9a\u7684\u6446\u653e\u4f4d\uff08\u5b57\u6bb5\uff09\u3002\u627e\u4e66\u4e0d\u9700\u8981\u4e00\u672c\u672c\u7ffb\u2014\u2014\u6309\u7f16\u53f7\uff08\u4e3b\u952e\uff09\u76f4\u63a5\u5b9a\u4f4d\u3002\u6587\u4ef6\u5b58\u50a8\u5219\u50cf\u628a\u4e66\u5168\u5806\u5728\u5bb6\u5730\u677f\u4e0a\uff1a\u4e66\u5c11\u8fd8\u884c\uff0c\u4e66\u591a\u4e86\u627e\u4e00\u672c\u8981\u7ffb\u534a\u5929\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u8fde\u63a5\u5185\u5b58\u6570\u636e\u5e93 + \u5efa\u8868 + \u63d2\u5165<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3\n\n# \u8fde\u63a5\u5185\u5b58\u6570\u636e\u5e93\uff08:memory: \u8868\u793a\u6570\u636e\u53ea\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u7a0b\u5e8f\u7ed3\u675f\u81ea\u52a8\u6d88\u5931\uff0c\u9002\u5408\u7ec3\u4e60\uff09\nconn = sqlite3.connect(\":memory:\")\ncursor = conn.cursor()\n\n# \u5efa\u8868\uff1a\u5148\u5b9a\u4e49\"\u8fd9\u672c\u4e66\u6709\u4ec0\u4e48\u5b57\u6bb5\"\uff0cIF NOT EXISTS \u8868\u793a\"\u5df2\u5b58\u5728\u5c31\u4e0d\u91cd\u590d\u5efa\"\ncursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS books (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,   -- \u81ea\u589e\u4e3b\u952e\uff0c\u552f\u4e00\u8eab\u4efd\u8bc1\n    title TEXT NOT NULL,                    -- \u4e66\u540d\uff0cNOT NULL \u4e0d\u80fd\u4e3a\u7a7a\n    author TEXT,                            -- \u4f5c\u8005\n    price REAL,                             -- \u4ef7\u683c\uff08\u5c0f\u6570\uff09\n    year INTEGER                            -- \u51fa\u7248\u5e74\u4efd\n)\n\"\"\")\n\n# \u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff08? \u662f\u5360\u4f4d\u7b26\uff0c\u503c\u653e\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u91cc\uff09\ncursor.execute(\n    \"INSERT INTO books (title, author, price, year) VALUES (?, ?, ?, ?)\",\n    (\"\u4e09\u4f53\", \"\u5218\u6148\u6b23\", 68.0, 2008),\n)\ncursor.execute(\"SELECT * FROM books\")\nprint(cursor.fetchall())\n\nconn.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;(1, '\u4e09\u4f53', '\u5218\u6148\u6b23', 68.0, 2008)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>connect(\":memory:\")<\/code> \u521b\u5efa\u5185\u5b58\u5e93\uff08\u4e0d\u843d\u76d8\uff0c\u7ec3\u4e60\u96f6\u6b8b\u7559\uff09\uff1b<code>cursor<\/code> \u662f\u300c\u6267\u884c SQL \u7684\u624b\u300d\uff1b<code>CREATE TABLE<\/code> \u91cc\u5148\u60f3\u597d\u5b57\u6bb5\uff1a<code>id<\/code> \u7528 <code>AUTOINCREMENT<\/code> \u81ea\u52a8\u7f16\u53f7\uff0c<code>title<\/code> \u6807 <code>NOT NULL<\/code> \u4fdd\u8bc1\u4e66\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff1b\u63d2\u5165\u65f6 <code>?<\/code> \u5360\u4f4d\u3001\u503c\u4ece\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165\uff1b<code>fetchall()<\/code> \u628a\u7ed3\u679c\u53d6\u6210\u5217\u8868\uff0c\u6bcf\u884c\u662f\u4e00\u4e2a\u5143\u7ec4\u3002<strong>\u5efa\u8868\u524d\u5148\u82b1 1 \u5206\u949f\u5728\u7eb8\u4e0a\u5199\u5b57\u6bb5\u6e05\u5355\uff0c\u6bd4\u76f4\u63a5\u6572\u4ee3\u7801\u5feb\u5f97\u591a<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5b57\u6bb5\u7c7b\u578b\u4e71\u5199\uff08\u5982 <code>price TEXT<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u5c0f\u6570\u6309\u6587\u672c\u5b58\uff0c\u6392\u5e8f\/\u8ba1\u7b97\u90fd\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u91d1\u989d\u7528 <code>REAL<\/code>\uff0c\u6574\u6570 <code>INTEGER<\/code>\uff0c\u6587\u672c <code>TEXT<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u4e86\u4e3b\u952e \u2192 \u95ee\u9898\uff1a\u65e0\u6cd5\u552f\u4e00\u6807\u8bc6\u4e00\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6bcf\u5f20\u8868\u90fd\u8bbe\u4e3b\u952e\uff0c\u4e00\u822c <code>id INTEGER PRIMARY KEY AUTOINCREMENT<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u91cd\u590d\u8fd0\u884c <code>CREATE TABLE<\/code> \u62a5\u5df2\u5b58\u5728 \u2192 \u95ee\u9898\uff1a\u7b2c\u4e8c\u6b21\u8fd0\u884c\u62a5\u300ctable already exists\u300d \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u52a0 <code>IF NOT EXISTS<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5e93\u88c5\u8868\u3001\u8868\u88c5\u884c\uff1b\u5efa\u8868\u5148\u5b9a\u5b57\u6bb5\u540d\uff0c\u4e3b\u952e\u552f\u4e00\u4e0d\u80fd\u5fd8\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 \u8fde\u63a5\u4e0e\u6e38\u6807 \u2014\u2014 sqlite3 \u7684\u300c\u4e09\u677f\u65a7\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sqlite3<\/code> \u7684\u4e09\u4e2a\u56fa\u5b9a\u52a8\u4f5c\uff1a<strong>\u8fde\u63a5\uff08connect\uff09\u2192 \u6e38\u6807\uff08cursor\uff09\u2192 \u6267\u884c\uff08execute\uff09<\/strong>\u3002\u8fde\u63a5\u662f\u300c\u901a\u5411\u6570\u636e\u5e93\u6587\u4ef6\u7684\u7ba1\u9053\u300d\uff0c\u6e38\u6807\u662f\u300c\u7ba1\u9053\u91cc\u64cd\u4f5c\u6570\u636e\u7684\u624b\u300d\u3002\u51e0\u4e4e\u6240\u6709\u6570\u636e\u5e93\u4ee3\u7801\u90fd\u957f\u4e00\u4e2a\u6837\uff0c\u8bb0\u4f4f\u4e09\u677f\u65a7\uff0cSQLite \u5c31\u5165\u95e8\u4e86\u4e00\u534a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>conn = sqlite3.connect(...)<\/code> \u65f6\uff0cPython \u6253\u5f00\u6570\u636e\u5e93\u6587\u4ef6\uff08\u6216\u5185\u5b58\u5e93\uff09\u5e76\u6301\u6709\u6587\u4ef6\u53e5\u67c4\uff1b<code>conn.cursor()<\/code> \u521b\u5efa\u6e38\u6807\u2014\u2014SQLite \u5f15\u64ce\u6267\u884c SQL \u540e\uff0c\u7ed3\u679c\u96c6\uff08result set\uff09\u5c31\u5b58\u653e\u5728\u6e38\u6807\u91cc\uff1b<code>fetchall()<\/code> \/ <code>fetchone()<\/code> \u628a\u7ed3\u679c\u300c\u8bfb\u51fa\u6765\u300d\u3002\u6240\u4ee5\uff1a<strong>execute \u53ea\u8d1f\u8d23\u6267\u884c\uff0c\u53d6\u7ed3\u679c\u5fc5\u987b\u7528 fetch \u7cfb\u5217<\/strong>\u3002\u8fde\u63a5\u9ed8\u8ba4\u5f00\u542f\u4e8b\u52a1\u7ba1\u7406\uff08\u89c1\u77e5\u8bc6\u70b9 4\uff09\uff0c\u5199\u64cd\u4f5c\u540e\u8981 <code>commit()<\/code> \u624d\u771f\u6b63\u843d\u76d8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fde\u63a5\u50cf<strong>\u81ea\u6765\u6c34\u7ba1\u9053<\/strong>\uff1a\u628a\u6c34\u5382\uff08\u6570\u636e\u5e93\u6587\u4ef6\uff09\u548c\u5bb6\uff08\u7a0b\u5e8f\uff09\u63a5\u901a\uff1b\u6e38\u6807\u50cf<strong>\u6c34\u9f99\u5934<\/strong>\uff1a\u6253\u5f00\u6c34\u9f99\u5934\uff08execute\uff09\u6c34\u624d\u6d41\u51fa\u6765\uff0c\u7528\u676f\u5b50\u63a5\u6c34\uff08fetchall\uff09\u624d\u80fd\u559d\u5230\u3002\u6c34\u7ba1\u4e0d\u5173\uff08\u4e0d <code>close()<\/code>\uff09\u4f1a\u6f0f\u6c34\u5360\u8d44\u6e90\uff1b\u6c34\u9f99\u5934\u4e0d\u63a5\uff08\u4e0d fetch\uff09\u6c34\u5c31\u767d\u767d\u6d41\u8d70\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u8fde\u63a5 + \u6e38\u6807 + \u6279\u91cf\u63d2\u5165 + \u6392\u5e8f\u67e5\u8be2<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3\n\nconn = sqlite3.connect(\":memory:\")\ncur = conn.cursor()\ncur.execute(\"CREATE TABLE t (name TEXT, score INTEGER)\")\n\nrows = &#91;(\"\u5c0f\u660e\", 90), (\"\u5c0f\u7ea2\", 85), (\"\u5c0f\u521a\", 78)]\ncur.executemany(\"INSERT INTO t VALUES (?, ?)\", rows)   # \u6279\u91cf\u63d2\u5165\uff0c\u6bd4\u5faa\u73af\u9ad8\u6548\n\ncur.execute(\"SELECT * FROM t ORDER BY score DESC\")    # \u6309\u5206\u6570\u4ece\u9ad8\u5230\u4f4e\nprint(cur.fetchall())\nconn.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;('\u5c0f\u660e', 90), ('\u5c0f\u7ea2', 85), ('\u5c0f\u521a', 78)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>executemany<\/code> \u63a5\u6536\u300c\u4e00\u6761\u5e26\u5360\u4f4d\u7b26\u7684 SQL + \u4e00\u4e2a\u5217\u8868\u300d\uff0cSQLite \u4f1a\u6279\u91cf\u6267\u884c\uff0c\u6bd4 for \u5faa\u73af\u4e00\u6761\u6761 execute \u5feb\u4e00\u4e2a\u6570\u91cf\u7ea7\uff1b<code>ORDER BY score DESC<\/code> \u5728\u6570\u636e\u5e93\u5185\u90e8\u6392\u5e8f\uff1b<code>fetchall()<\/code> \u4e00\u6b21\u53d6\u56de\u6240\u6709\u884c\u3002<strong>\u53d6\u6570\u636e\u65f6\u6ce8\u610f\uff1afetchall \u6bcf\u8c03\u7528\u4e00\u6b21\uff0c\u6e38\u6807\u91cc\u7684\u7ed3\u679c\u5c31\u5c11\u4e00\u6279\uff0c\u91cd\u590d\u8c03\u7528\u4f1a\u8fd4\u56de\u7a7a\u5217\u8868<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528\u5b8c\u4e0d <code>conn.close()<\/code> \u2192 \u95ee\u9898\uff1a\u6587\u4ef6\u53e5\u67c4\u5360\u7528\uff0cWindows \u4e0a\u53ef\u80fd\u9501\u4f4f db \u6587\u4ef6\uff0c\u5220\u4e0d\u6389 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528\u5b8c <code>conn.close()<\/code>\uff1b\u6216\u7528 <code>with sqlite3.connect(...) as conn:<\/code> \u81ea\u52a8\u5173\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>fetchall()<\/code> \u8fde\u7eed\u8c03\u7528\u4e24\u6b21 \u2192 \u95ee\u9898\uff1a\u7b2c\u4e8c\u6b21\u8fd4\u56de <code>[]<\/code>\uff0c\u7ed3\u679c\u53d6\u8fc7\u4e00\u6b21\u5c31\u6ca1\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e00\u6b21\u53d6\u5b8c\u5b58\u8fdb\u53d8\u91cf\uff1a<code>rows = cur.fetchall()<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea execute \u4e0d fetch\uff0c\u53c8\u60f3\u62ff\u7ed3\u679c \u2192 \u95ee\u9898\uff1a\u7ed3\u679c\u96c6\u5728\u6e38\u6807\u91cc\u6ca1\u4eba\u8bfb \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u67e5\u8be2\u540e\u5fc5\u987b <code>fetchall()<\/code> \/ <code>fetchone()<\/code> \/ <code>fetchmany()<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u8fde\u63a5\u5f00\u7ba1\u9053\uff0c\u6e38\u6807\u6765\u64cd\u4f5c\uff0cexecute \u6267\u884c\u3001fetch \u53d6\u7ed3\u679c\uff0c\u7528\u5b8c\u90fd\u8981\u5173\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 \u53c2\u6570\u5316\u67e5\u8be2 \u2014\u2014 \u4e3a\u4ec0\u4e48\u7528 <code>?<\/code> \u800c\u4e0d\u662f\u62fc\u63a5\u5b57\u7b26\u4e32<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u6570\u5316\u67e5\u8be2<\/strong>\uff08parameterized query\uff09\uff1a<strong>SQL \u91cc\u7528 <code>?<\/code> \u5360\u4f4d\uff0c\u503c\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5355\u72ec\u4f20\u5165<\/strong>\uff0c\u800c\u4e0d\u662f\u628a\u503c\u62fc\u8fdb SQL \u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u4eca\u5929\u6700\u91cd\u8981\u7684\u5b89\u5168\u77e5\u8bc6\u70b9\u2014\u2014\u6570\u636e\u5e93\u6700\u5e38\u89c1\u7684\u653b\u51fb\u65b9\u5f0f SQL \u6ce8\u5165\uff08SQL injection\uff0c\u653b\u51fb\u8005\u628a\u6076\u610f SQL \u4ee3\u7801\u4f2a\u88c5\u6210\u8f93\u5165\u6570\u636e\uff09\uff0c\u5c31\u662f\u9760\u300c\u4e0d\u62fc\u63a5\u300d\u9632\u4f4f\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u5e93\u5904\u7406 SQL \u5206\u4e24\u6b65\uff1a\u5148<strong>\u89e3\u6790\u7f16\u8bd1<\/strong>\uff08\u628a\u5b57\u7b26\u4e32\u53d8\u6210\u6267\u884c\u8ba1\u5212\uff09\uff0c\u518d<strong>\u6267\u884c<\/strong>\u3002\u62fc\u63a5\u5199\u6cd5\u8ba9\u7528\u6237\u8f93\u5165\u53c2\u4e0e\u300c\u89e3\u6790\u300d\u2014\u2014\u8f93\u5165\u53d8\u6210\u4e86 SQL \u8bed\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u653b\u51fb\u8005\u5c31\u80fd\u6539\u5199\u4f60\u7684 SQL\uff1b\u53c2\u6570\u5316\u5199\u6cd5\u8ba9\u7528\u6237\u8f93\u5165\u53ea\u53c2\u4e0e\u300c\u6267\u884c\u300d\u2014\u2014\u6570\u636e\u5e93\u628a <code>?<\/code> \u7684\u4f4d\u7f6e\u56fa\u5b9a\u4e3a\u300c\u4e00\u4e2a\u503c\u300d\uff0c\u4f20\u5165\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u88ab\u5f53\u4f5c\u6570\u636e\uff08\u54ea\u6015\u91cc\u9762\u5199\u7740 <code>DROP TABLE<\/code> \u4e5f\u53ea\u662f\u4e2a\u666e\u901a\u5b57\u7b26\u4e32\uff09\u3002<strong>\u540c\u6837\u662f\u6267\u884c\uff0c\u53c2\u4e0e\u54ea\u4e2a\u9636\u6bb5\uff0c\u51b3\u5b9a\u4e86\u5b89\u5168\u8fd8\u662f\u5371\u9669<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u62fc\u63a5 SQL \u50cf<strong>\u628a\u6536\u8d27\u5730\u5740\u76f4\u63a5\u5370\u5728\u5feb\u9012\u5355\u6a21\u677f\u4e0a<\/strong>\u2014\u2014\u7528\u6237\u5199\u300c\u65b0\u7586\u300d\uff0c\u5feb\u9012\u5355\u5c31\u53d8\u6210\u300c\u53d1\u5f80\u65b0\u7586\u300d\uff1b\u7528\u6237\u5982\u679c\u5199\u300c\u65b0\u7586\uff1b\u987a\u4fbf\u628a\u8f66\u5f00\u8d70\u300d\uff0c\u8fd9\u53e5\u6307\u4ee4\u4e5f\u4f1a\u88ab\u7167\u5355\u6267\u884c\u3002\u53c2\u6570\u5316\u67e5\u8be2\u5219\u50cf<strong>\u5feb\u9012\u5355\u548c\u8d27\u7269\u5206\u5f00<\/strong>\uff1a\u5730\u5740\u680f\u6c38\u8fdc\u662f\u300c\u4e00\u4e2a\u5730\u5740\u300d\u7684\u4f4d\u7f6e\uff0c\u586b\u4ec0\u4e48\u90fd\u53ea\u662f\u5730\u5740\uff0c\u6c38\u8fdc\u53d8\u4e0d\u6210\u989d\u5916\u6307\u4ee4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u62fc\u63a5 vs \u53c2\u6570\u5316\u7684\u5bf9\u6bd4\u5b9e\u9a8c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3\n\nconn = sqlite3.connect(\":memory:\")\ncur = conn.cursor()\ncur.execute(\"CREATE TABLE users (name TEXT, password TEXT)\")\ncur.execute(\"INSERT INTO users VALUES ('admin', 'secret123')\")\ncur.executemany(\"INSERT INTO users VALUES (?, ?)\", &#91;(\"alice\", \"abc\"), (\"bob\", \"xyz\")])\n\nuser_input = \"' OR '1'='1\"                                  # \u6a21\u62df\u6076\u610f\u8f93\u5165\nsql = f\"SELECT * FROM users WHERE name = '{user_input}'\"    # \u9519\u8bef\u505a\u6cd5\uff1a\u62fc\u63a5\nprint(\"\u62fc\u63a5\u7248\u67e5\u51fa\", len(cur.execute(sql).fetchall()), \"\u884c\")\n\ncur.execute(\"SELECT * FROM users WHERE name = ?\", (user_input,))  # \u6b63\u786e\u505a\u6cd5\uff1a\u53c2\u6570\u5316\nprint(\"\u53c2\u6570\u5316\u7248\u67e5\u51fa\", len(cur.fetchall()), \"\u884c\")\nconn.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u62fc\u63a5\u7248\u67e5\u51fa 3 \u884c\n\u53c2\u6570\u5316\u7248\u67e5\u51fa 0 \u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u6237\u8f93\u5165 <code>' OR '1'='1<\/code>\uff0c\u62fc\u63a5\u540e SQL \u53d8\u6210 <code>WHERE name = '' OR '1'='1'<\/code>\u2014\u2014\u6761\u4ef6\u6c38\u8fdc\u6210\u7acb\uff0c3 \u4e2a\u7528\u6237\u5168\u88ab\u67e5\u51fa\u6765\uff08\u6ce8\u5165\u6210\u529f\uff01\uff09\uff1b\u6362\u6210\u53c2\u6570\u5316\u67e5\u8be2\u540e\uff0c\u540c\u6837\u7684\u8f93\u5165\u88ab\u6570\u636e\u5e93\u5f53\u6210\u300c\u4e00\u4e2a\u5b57\u9762\u503c\u300d\uff0c\u67e5\u51fa 0 \u884c\uff08\u6ce8\u5165\u5931\u6548\uff09\u3002<strong>\u540c\u4e00\u4efd\u8f93\u5165\uff0c\u4e24\u79cd\u5199\u6cd5\uff0c\u5929\u58e4\u4e4b\u522b<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6a21\u7cca\u641c\u7d22\u7684\u6807\u51c6\u5199\u6cd5\uff08LIKE + \u53c2\u6570\u5316\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>keyword = input(\"\u8f93\u5165\u4e66\u540d\u5173\u952e\u8bcd\uff1a\")\ncursor.execute(\"SELECT title, author FROM books WHERE title LIKE ?\", (f\"%{keyword}%\",))\nrows = cursor.fetchall()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LIKE<\/code> \u662f\u6a21\u7cca\u5339\u914d\uff0c<code>%\u5173\u952e\u5b57%<\/code> \u8868\u793a\u300c\u4e2d\u95f4\u5305\u542b\u5173\u952e\u5b57\u300d\u3002<strong>\u89c4\u5219\uff1a\u51e1\u662f SQL \u91cc\u6709\u6765\u81ea\u5916\u90e8\u7684\u503c\uff0c\u4e00\u5f8b\u7528 <code>?<\/code> \u5360\u4f4d\u4f20\u53c2\uff0c\u7edd\u4e0d\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>f\"... WHERE title = '{title}'\"<\/code> \u62fc\u63a5 \u2192 \u95ee\u9898\uff1a\u7528\u6237\u8f93\u5165\u53d8\u6210 SQL \u4ee3\u7801\uff0c\u53ef\u6ce8\u5165\u3001\u53ef\u5220\u8868 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e00\u5f8b <code>?<\/code> \u5360\u4f4d + \u5143\u7ec4\u4f20\u53c2\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>?<\/code> \u4e2a\u6570\u548c\u53c2\u6570\u4e2a\u6570\u5bf9\u4e0d\u4e0a \u2192 \u95ee\u9898\uff1a\u62a5 <code>Incorrect number of bindings<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6570\u6e05 <code>?<\/code>\uff0c\u53c2\u6570\u5143\u7ec4\u5143\u7d20\u4e2a\u6570\u4e00\u81f4\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5355\u503c\u5fd8\u4e86\u5199\u9017\u53f7\uff1a<code>(name)<\/code> \u2192 \u95ee\u9898\uff1a<code>(name)<\/code> \u53ea\u662f\u62ec\u53f7\u4e0d\u662f\u5143\u7ec4 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5355\u503c\u4e5f\u8981\u5199 <code>(name,)<\/code>\uff08\u9017\u53f7\u624d\u662f\u5143\u7ec4\uff09\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>?<\/code> \u7528\u5728\u8868\u540d\/\u5217\u540d\uff08\u5982 <code>FROM ?<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u8868\u540d\u3001\u5217\u540d\u4e0d\u80fd\u53c2\u6570\u5316 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8868\u540d\/\u5217\u540d\u5199\u6b7b\u5728 SQL\uff0c\u53ea\u6709\u300c\u503c\u300d\u80fd\u53c2\u6570\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aSQL \u5360\u4f4d\u7528\u95ee\u53f7\uff0c\u503c\u8d70\u53c2\u6570\u4e0d\u62fc\u63a5\uff1b\u6ce8\u5165\u6700\u6015\u8fd9\u53e5\u8bdd\uff0c\u5b89\u5168\u5e95\u7ebf\u8bb0\u5fc3\u95f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 \u4e8b\u52a1\u4e0e commit \u2014\u2014 \u4e3a\u4ec0\u4e48\u6539\u4e86\u6570\u636e\u6ca1\u751f\u6548<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8b\u52a1<\/strong>\uff08transaction\uff09\u662f<strong>\u4e00\u7ec4\u8981\u4e48\u5168\u90e8\u6210\u529f\u3001\u8981\u4e48\u5168\u90e8\u4e0d\u751f\u6548\u7684\u64cd\u4f5c<\/strong>\u3002<code>commit()<\/code> \u662f\u300c\u786e\u8ba4\u5b9a\u7a3f\u300d\uff0c<code>rollback()<\/code> \u662f\u300c\u5168\u90e8\u64a4\u9500\u300d\u3002\u65b0\u624b\u6700\u5e38\u9047\u5230\u7684\u602a\u73b0\u8c61\u300c\u660e\u660e UPDATE \u4e86\uff0c\u91cd\u542f\u7a0b\u5e8f\u6570\u636e\u8fd8\u662f\u8001\u7684\u300d\u2014\u2014\u5341\u6709\u516b\u4e5d\u662f\u5fd8\u4e86 <code>commit()<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SQLite \u9ed8\u8ba4\u5728\u300c\u56de\u6eda\u65e5\u5fd7\u300d\u6a21\u5f0f\u4e0b\u5de5\u4f5c\uff1a\u5199\u64cd\u4f5c\uff08INSERT\/UPDATE\/DELETE\uff09\u5f00\u59cb\u524d\uff0c\u5148\u628a\u65e7\u6570\u636e\u5199\u8fdb\u65e5\u5fd7\u6587\u4ef6\uff0c\u4fee\u6539\u53ea\u5b58\u5728\u4e8e\u5185\u5b58\uff08\u5bf9\u5176\u4ed6\u8fde\u63a5\u4e0d\u53ef\u89c1\uff09\uff1b<code>commit()<\/code> \u65f6\u624d\u628a\u4fee\u6539\u771f\u6b63\u5199\u5165\u4e3b\u6570\u636e\u5e93\u6587\u4ef6\u5e76\u6e05\u7a7a\u65e5\u5fd7\uff1b\u5982\u679c\u5d29\u6e83\u6216 <code>rollback()<\/code>\uff0c\u5c31\u7528\u65e5\u5fd7\u628a\u65e7\u6570\u636e\u8fd8\u539f\u3002\u6240\u4ee5<strong>\u672a commit \u7684\u4fee\u6539\uff1a\u81ea\u5df1\u7684\u8fde\u63a5\u80fd\u770b\u5230\uff0c\u5176\u4ed6\u8fde\u63a5\u770b\u4e0d\u5230\uff0c\u7a0b\u5e8f\u9000\u51fa\u5c31\u4e22<\/strong>\u2014\u2014\u8fd9\u5c31\u662f\u300c\u6539\u4e86\u6ca1\u751f\u6548\u300d\u7684\u771f\u76f8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8b\u52a1\u50cf<strong>\u94f6\u884c\u8f6c\u8d26<\/strong>\uff1a\u4ece A \u6263 100\u3001\u7ed9 B \u52a0 100\uff0c\u4e24\u6b65\u5fc5\u987b\u90fd\u6210\u529f\uff1b\u4e2d\u95f4\u4efb\u4f55\u4e00\u6b65\u5931\u8d25\uff08\u6bd4\u5982\u7f51\u7edc\u65ad\u4e86\uff09\uff0c\u94f6\u884c\u4e0d\u80fd\u53ea\u6263\u94b1\u4e0d\u6253\u94b1\u2014\u2014\u5fc5\u987b\u6574\u4f53\u64a4\u9500\u3002<code>commit<\/code> \u5c31\u662f\u67dc\u5458\u7684\u300c\u786e\u8ba4\u300d\u6309\u94ae\uff1a\u4e0d\u6309\u786e\u8ba4\uff0c\u8fd9\u7b14\u64cd\u4f5c\u53ea\u662f\u300c\u5f85\u529e\u8349\u7a3f\u300d\uff1b\u6309\u4e86\u786e\u8ba4\uff0c\u624d\u771f\u6b63\u751f\u6548\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>commit \u8ba9\u4fee\u6539\u771f\u6b63\u751f\u6548\uff08\u8f6c\u8d26\u4e8b\u52a1\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3\n\nconn = sqlite3.connect(\":memory:\")\ncur = conn.cursor()\ncur.execute(\"CREATE TABLE account (name TEXT, money REAL)\")\ncur.execute(\"INSERT INTO account VALUES ('\u7532', 1000), ('\u4e59', 1000)\")\ncur.execute(\"UPDATE account SET money = money - 100 WHERE name = '\u7532'\")\ncur.execute(\"UPDATE account SET money = money + 100 WHERE name = '\u4e59'\")\nconn.commit()\nprint(cur.execute(\"SELECT name, money FROM account\").fetchall())\nconn.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;('\u7532', 900.0), ('\u4e59', 1100.0)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u4e2d\u9014\u51fa\u9519 \u2192 rollback \u5168\u90e8\u64a4\u9500\uff0c\u4e0d\u7559\u534a\u622a\u6570\u636e<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conn = sqlite3.connect(\":memory:\")\ncur = conn.cursor()\ncur.execute(\"CREATE TABLE account (name TEXT, money REAL)\")\ncur.execute(\"INSERT INTO account VALUES ('\u7532', 1000), ('\u4e59', 1000)\")\nconn.commit()  # \u5148\u63d0\u4ea4\u521d\u59cb\u6570\u636e\ntry:\n    cur.execute(\"UPDATE account SET money = money - 100 WHERE name = '\u7532'\")\n    cur.execute(\"UPDATE account SET nope = 1 WHERE name = '\u4e59'\")  # \u6545\u610f\u5199\u9519\u5b57\u6bb5\u540d\n    conn.commit()\nexcept Exception as e:\n    conn.rollback()  # \u64a4\u9500\u672c\u4e8b\u52a1\u7684\u6240\u6709\u4fee\u6539\n    print(\"\u51fa\u9519\u5df2\u56de\u6eda\uff1a\", e)\nprint(cur.execute(\"SELECT name, money FROM account\").fetchall())\nconn.close()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u51fa\u9519\u5df2\u56de\u6eda\uff1a no such column: nope\n&#91;('\u7532', 1000.0), ('\u4e59', 1000.0)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u6761 UPDATE \u5b57\u6bb5\u540d\u5199\u9519\u629b\u5f02\u5e38\uff0c<code>except<\/code> \u91cc <code>rollback()<\/code> \u628a\u7b2c\u4e00\u6761\u4e5f\u64a4\u9500\uff0c\u8d26\u6237\u6062\u590d\u539f\u6837\uff08<strong>\u8981\u4e48\u4e00\u8d77\u6210\u529f\uff0c\u8981\u4e48\u4e00\u8d77\u4e0d\u7559<\/strong>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u4e0d commit \u7684\u540e\u679c\u2014\u2014\u53e6\u4e00\u4e2a\u8fde\u63a5\u4ec0\u4e48\u90fd\u770b\u4e0d\u5230<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db_path = os.path.join(tempfile.gettempdir(), \"demo_no_commit.db\")\nconn = sqlite3.connect(db_path)\ncur = conn.cursor()\ncur.execute(\"CREATE TABLE account (name TEXT, money REAL)\")\ncur.execute(\"INSERT INTO account VALUES ('\u7532', 1000)\")\ncur.execute(\"UPDATE account SET money = 0 WHERE name = '\u7532'\")  # \u6ca1 commit\uff01\nconn2 = sqlite3.connect(db_path)  # \u7b2c\u4e8c\u4e2a\u8fde\u63a5\u770b\nprint(\"\u672a commit\uff0c\u53e6\u4e00\u4e2a\u8fde\u63a5\u770b\u5230\uff1a\", conn2.execute(\"SELECT name, money FROM account\").fetchall())\nconn.commit()\nprint(\"commit \u540e\uff0c\u53e6\u4e00\u4e2a\u8fde\u63a5\u770b\u5230\uff1a\", conn2.execute(\"SELECT name, money FROM account\").fetchall())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u672a commit\uff0c\u53e6\u4e00\u4e2a\u8fde\u63a5\u770b\u5230\uff1a &#91;]\ncommit \u540e\uff0c\u53e6\u4e00\u4e2a\u8fde\u63a5\u770b\u5230\uff1a &#91;('\u7532', 0.0)]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u4f4f\uff1a\u589e\u5220\u6539\u4e4b\u540e\uff0c<code>commit()<\/code> \u4e0d\u662f\u53ef\u9009\u9879\uff0c\u662f\u5fc5\u9009\u9879<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u589e\u5220\u6539\u540e\u5fd8\u4e86 <code>conn.commit()<\/code> \u2192 \u95ee\u9898\uff1a\u6570\u636e\u300c\u6539\u4e86\u6ca1\u751f\u6548\u300d\uff0c\u91cd\u5f00\u7a0b\u5e8f\u5c31\u6ca1\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6bcf\u6b21\u589e\u5220\u6539\u540e\u6267\u884c <code>conn.commit()<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>try\/except<\/code> \u91cc\u5fd8\u4e86 <code>rollback()<\/code> \u2192 \u95ee\u9898\uff1a\u51fa\u9519\u540e\u6570\u636e\u534a\u65b0\u534a\u65e7\uff0c\u7559\u4e0b\u810f\u6570\u636e \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>except<\/code> \u5206\u652f\u91cc\u5fc5\u987b <code>conn.rollback()<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u6bcf\u6761 execute \u90fd commit \u2192 \u95ee\u9898\uff1a\u6027\u80fd\u5dee\uff08\u6bcf\u6b21\u5199\u76d8\uff09\uff0c\u4e8b\u52a1\u7684\u610f\u4e49\u4e5f\u6ca1\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e00\u7ec4\u76f8\u5173\u64cd\u4f5c\u4e00\u4e2a\u4e8b\u52a1\uff0c\u6700\u540e commit \u4e00\u6b21\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u589e\u5220\u6539\u540e\u8981 commit\uff0c\u51fa\u9519 rollback \u5168\u64a4\u9500\uff1b\u4e00\u8d77\u6210\u529f\u4e00\u8d77\u64a4\uff0c\u4e8b\u52a1\u4fdd\u9669\u4e0d\u51fa\u9519\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u52a8\u624b\u5b9e\u8df5\uff1a\u56fe\u4e66\u7ba1\u7406\u6570\u636e\u5e93<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u5b8c\u6574\u7684\u300c\u56fe\u4e66\u7ba1\u7406\u300d\u7a0b\u5e8f\uff1a\u6dfb\u52a0\u3001\u663e\u793a\u5168\u90e8\u3001\u6309\u4ef7\u683c\u533a\u95f4\u67e5\u8be2\u3001\u5220\u9664\uff0c\u5168\u90e8\u8d70\u53c2\u6570\u5316\u67e5\u8be2 + \u4e8b\u52a1\u63d0\u4ea4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># book_manager.py \u2014\u2014 \u56fe\u4e66\u7ba1\u7406\u6570\u636e\u5e93\nimport sqlite3\n\nDB_FILE = \"books.db\"\n\ndef get_conn():\n    \"\"\"\u5efa\u8868\u5e76\u8fd4\u56de\u8fde\u63a5\uff08\u6bcf\u6b65\u72ec\u7acb\u8fde\u63a5\uff0c\u7528\u5b8c\u5173\u95ed\uff09\"\"\"\n    conn = sqlite3.connect(DB_FILE)\n    conn.execute(\"\"\"\n    CREATE TABLE IF NOT EXISTS books (\n        id INTEGER PRIMARY KEY AUTOINCREMENT,\n        title TEXT NOT NULL,\n        author TEXT,\n        price REAL,\n        year INTEGER\n    )\n    \"\"\")\n    return conn\n\ndef add_book(title, author, price, year):\n    conn = get_conn()\n    conn.execute(\"INSERT INTO books (title, author, price, year) VALUES (?, ?, ?, ?)\",\n                 (title, author, price, year))\n    conn.commit()\n    conn.close()\n    print(f\"\u5df2\u6dfb\u52a0\u300a{title}\u300b\")\n\ndef show_all():\n    conn = get_conn()\n    rows = conn.execute(\"SELECT id, title, author, price, year FROM books\").fetchall()\n    conn.close()\n    if not rows:\n        print(\"\u4e66\u5e93\u4e3a\u7a7a\")\n        return\n    print(f\"{'ID':&lt;4}{'\u4e66\u540d':&lt;28}{'\u4f5c\u8005':&lt;20}{'\u4ef7\u683c':&lt;8}{'\u5e74\u4efd'}\")\n    print(\"-\" * 70)\n    for id_, title, author, price, year in rows:\n        print(f\"{id_:&lt;4}{title:&lt;28}{str(author):&lt;20}{price:&lt;8}{year}\")\n\ndef query_by_price(low, high):\n    conn = get_conn()\n    rows = conn.execute(\n        \"SELECT title, price, year FROM books WHERE price BETWEEN ? AND ? ORDER BY price\",\n        (low, high),\n    ).fetchall()\n    conn.close()\n    if not rows:\n        print(f\"\u6ca1\u6709\u4ef7\u683c\u5728 {low}~{high} \u5143\u7684\u4e66\")\n        return\n    for title, price, year in rows:\n        print(f\"\u300a{title}\u300b {price} \u5143\uff08{year} \u5e74\uff09\")\n\ndef delete_book(book_id):\n    conn = get_conn()\n    conn.execute(\"DELETE FROM books WHERE id = ?\", (book_id,))\n    conn.commit()\n    conn.close()\n    print(f\"\u5df2\u5220\u9664 ID={book_id} \u7684\u4e66\")\n\ndef main():\n    while True:\n        print(\"\\n1.\u6dfb\u52a0\u56fe\u4e66  2.\u663e\u793a\u5168\u90e8  3.\u4ef7\u683c\u67e5\u8be2  4.\u5220\u9664  5.\u9000\u51fa\")\n        choice = input(\"\u8bf7\u9009\u62e9\uff1a\").strip()\n        if choice == \"1\":\n            title = input(\"\u4e66\u540d\uff1a\")\n            author = input(\"\u4f5c\u8005\uff1a\")\n            price = float(input(\"\u4ef7\u683c\uff1a\"))\n            year = int(input(\"\u51fa\u7248\u5e74\u4efd\uff1a\"))\n            add_book(title, author, price, year)\n        elif choice == \"2\":\n            show_all()\n        elif choice == \"3\":\n            low = float(input(\"\u6700\u4f4e\u4ef7\u683c\uff1a\"))\n            high = float(input(\"\u6700\u9ad8\u4ef7\u683c\uff1a\"))\n            query_by_price(low, high)\n        elif choice == \"4\":\n            show_all()\n            delete_book(int(input(\"\u8f93\u5165\u8981\u5220\u9664\u7684 ID\uff1a\")))\n        elif choice == \"5\":\n            print(\"\u518d\u89c1\")\n            break\n        else:\n            print(\"\u65e0\u6548\u9009\u62e9\")\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.\u6dfb\u52a0\u56fe\u4e66  2.\u663e\u793a\u5168\u90e8  3.\u4ef7\u683c\u67e5\u8be2  4.\u5220\u9664  5.\u9000\u51fa\n\u8bf7\u9009\u62e9\uff1a1\n\u4e66\u540d\uff1a\u4e09\u4f53\n\u4f5c\u8005\uff1a\u5218\u6148\u6b23\n\u4ef7\u683c\uff1a68\n\u51fa\u7248\u5e74\u4efd\uff1a2008\n\u5df2\u6dfb\u52a0\u300a\u4e09\u4f53\u300b\n\u8bf7\u9009\u62e9\uff1a2\nID  \u4e66\u540d                          \u4f5c\u8005                  \u4ef7\u683c     \u5e74\u4efd\n----------------------------------------------------------------------\n3   Python \u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5        Eric Matthes          89.0     2020\n...\n8   \u4e09\u4f53                          \u5218\u6148\u6b23                68.0     2008\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9000\u51fa\u7a0b\u5e8f\u540e\u518d\u542f\u52a8\uff0c\u6570\u636e\u8fd8\u5728\u2014\u2014\u8fd9\u5c31\u662f\u6570\u636e\u5e93\u76f8\u6bd4\u666e\u901a\u6587\u4ef6\u7684\u4ef7\u503c\uff1a<strong>\u6301\u4e45\u5316 + \u7ed3\u6784\u5316 + \u53ef\u67e5\u8be2<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u52a0\u4e00\u4e2a\u300c\u6309\u4f5c\u8005\u641c\u7d22\u300d\u529f\u80fd\uff08<code>WHERE author = ?<\/code>\uff09\uff0c\u5e76\u628a\u641c\u7d22\u5173\u952e\u8bcd\u7528 <code>LIKE<\/code> \u6539\u6210\u6a21\u7cca\u5339\u914d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u80fd\u8bf4\u6e05\u300c\u6570\u636e\u5e93 \u2192 \u8868 \u2192 \u5b57\u6bb5 \u2192 \u8bb0\u5f55\u300d\u7684\u5c42\u7ea7\u5173\u7cfb\uff0c\u72ec\u7acb\u8bbe\u8ba1\u4e00\u5f20\u7b80\u5355\u7684\u8868\uff08\u60f3\u6e05\u695a\u5b57\u6bb5\u7c7b\u578b\uff1aREAL\/INTEGER\/TEXT\uff09\n\u7528\u6807\u51c6\u5e93 <code>sqlite3<\/code>\uff1a<code>connect<\/code> \u2192 <code>cursor<\/code> \u2192 <code>execute<\/code> \u2192 <code>fetch<\/code> \u4e09\u677f\u65a7\uff0c<code>executemany<\/code> \u6279\u91cf\u63d2\u5165\u66f4\u9ad8\u6548\n\u589e\u5220\u6539\u67e5 CRUD\uff1aINSERT \/ SELECT \/ UPDATE \/ DELETE\uff0c<code>WHERE<\/code> \u6761\u4ef6\u7b5b\u9009\u3001<code>ORDER BY<\/code> \u6392\u5e8f\u3001COUNT\/AVG\/SUM \u7edf\u8ba1\n\u53c2\u6570\u5316\u67e5\u8be2\uff1aSQL \u7528 <code>?<\/code> \u5360\u4f4d\u3001\u503c\u8d70\u53c2\u6570\uff0c\u7edd\u4e0d\u62fc\u63a5\u2014\u2014\u9632\u4f4f SQL \u6ce8\u5165\uff08\u540c\u4e00\u8f93\u5165\u62fc\u63a5\u7248\u67e5\u51fa 3 \u884c\u3001\u53c2\u6570\u5316\u7248\u67e5\u51fa 0 \u884c\uff09\n\u4e8b\u52a1\u673a\u5236\uff1a\u589e\u5220\u6539\u540e\u5fc5\u987b <code>commit()<\/code>\uff0c\u51fa\u9519 <code>rollback()<\/code> \u5168\u90e8\u64a4\u9500\uff1b\u300c\u8981\u4e48\u4e00\u8d77\u6210\u529f\u3001\u8981\u4e48\u4e00\u8d77\u4e0d\u7559\u300d\n\u56fe\u4e66\u7ba1\u7406\u6570\u636e\u5e93\u80fd\u6dfb\u52a0\u3001\u663e\u793a\u3001\u4ef7\u683c\u67e5\u8be2\u3001\u5220\u9664\uff0c\u6570\u636e\u91cd\u542f\u4e0d\u4e22<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>sqlite3.OperationalError: no such table: books<\/code><\/td><td>\u8868\u8fd8\u6ca1\u521b\u5efa\u5c31\u53bb\u67e5\u8be2<\/td><td>\u5148\u6267\u884c <code>CREATE TABLE IF NOT EXISTS<\/code> \u518d\u67e5\u8be2\uff1b\u68c0\u67e5\u6570\u636e\u5e93\u6587\u4ef6\u8def\u5f84\u662f\u5426\u4e00\u81f4<\/td><\/tr><tr><td><code>sqlite3.OperationalError: near \"?\"<\/code><\/td><td><code>?<\/code> \u5199\u5728\u4e86\u4e0d\u8be5\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u6216 SQL \u8bed\u6cd5\u9519\u8bef<\/td><td>\u68c0\u67e5 SQL \u8bed\u6cd5\uff1b\u786e\u8ba4\u5360\u4f4d\u7b26\u53ea\u51fa\u73b0\u5728 VALUES\/WHERE \u7b49\u503c\u7684\u4f4d\u7f6e<\/td><\/tr><tr><td><code>sqlite3.ProgrammingError: Incorrect number of bindings<\/code><\/td><td>\u5360\u4f4d\u7b26\u6570\u91cf\u4e0e\u4f20\u5165\u53c2\u6570\u6570\u91cf\u4e0d\u4e00\u81f4<\/td><td>\u6570\u4e00\u6570 <code>?<\/code> \u7684\u4e2a\u6570\uff0c\u548c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u5143\u7ec4\u5143\u7d20\u4e2a\u6570\u5bf9\u9f50<\/td><\/tr><tr><td>\u589e\u5220\u6539\u540e\u6570\u636e\u6ca1\u53d8<\/td><td>\u5fd8\u8bb0\u8c03\u7528 <code>conn.commit()<\/code><\/td><td>\u6bcf\u6b21\u589e\u5220\u6539\u540e\u6267\u884c <code>conn.commit()<\/code><\/td><\/tr><tr><td>\u67e5\u4e0d\u5230\u4e2d\u6587\u6570\u636e<\/td><td>\u5199\u5165\u548c\u67e5\u8be2\u7684\u7f16\u7801\u4e0d\u4e00\u81f4\uff0c\u6216\u503c\u91cc\u6709\u591a\u4f59\u7a7a\u683c<\/td><td>\u7edf\u4e00\u7528 <code>encoding=\"utf-8\"<\/code>\uff1b\u7528 <code>strip()<\/code> \u6e05\u7406\u8f93\u5165\uff1b<code>LIKE<\/code> \u6a21\u7cca\u5339\u914d\u4ee3\u66ff\u7cbe\u786e\u5339\u914d<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u6570\u636e\u5e93\u4e0e\u8868 \u2014\u2014 \u5148\u60f3\u6e05\u695a\u300c\u5b58\u4ec0\u4e48\u300d\uff0c\u518d\u52a8\u624b\u5efa\u8868 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u6570\u636e\u5e93\uff08database\uff09\u662f\u6709\u7ec4\u7ec7\u5730 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}