{"id":1171899,"date":"2025-01-15T16:41:24","date_gmt":"2025-01-15T08:41:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171899.html"},"modified":"2025-01-15T16:41:27","modified_gmt":"2025-01-15T08:41:27","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87python%e8%bf%90%e8%a1%8csql%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171899.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7python\u8fd0\u884csql\u8bed\u53e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074134\/6f940129-47f1-40ad-9af4-b37866cb9b7d.webp\" alt=\"\u5982\u4f55\u901a\u8fc7python\u8fd0\u884csql\u8bed\u53e5\" \/><\/p>\n<p><p> \u8981\u901a\u8fc7Python\u8fd0\u884cSQL\u8bed\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\uff0c\u6700\u5e38\u89c1\u7684\u5e93\u6709\uff1a<strong>sqlite3\u3001PyMySQL\u3001psycopg2<\/strong>\u3002\u5176\u4e2d\uff0c<strong>sqlite3<\/strong>\u9002\u7528\u4e8eSQLite\u6570\u636e\u5e93\uff0c<strong>PyMySQL<\/strong>\u9002\u7528\u4e8eMySQL\u6570\u636e\u5e93\uff0c<strong>psycopg2<\/strong>\u9002\u7528\u4e8ePostgreSQL\u6570\u636e\u5e93\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u8fd9\u4e09\u4e2a\u5e93\u8fd0\u884cSQL\u8bed\u53e5\u7684\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u5c55\u5f00<strong>sqlite3<\/strong>\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528sqlite3\u8fd0\u884cSQL\u8bed\u53e5<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165sqlite3\u5e93<\/h3>\n<\/p>\n<p><p><strong>sqlite3<\/strong>\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff0c\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6216\u8fde\u63a5\u5230\u4e00\u4e2aSQLite\u6570\u636e\u5e93\u3002\u5982\u679c\u6570\u636e\u5e93\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u6267\u884cSQL\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u6267\u884cSQL\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u8bed\u53e5\uff0c\u4f8b\u5982\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u3001\u67e5\u8be2\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u8868<\/p>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Bob&#39;, 25)&quot;)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u63d0\u4ea4\u4e8b\u52a1<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u9700\u8981\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528PyMySQL\u8fd0\u884cSQL\u8bed\u53e5<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5PyMySQL\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5PyMySQL\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyMySQL<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5bfc\u5165PyMySQL\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    db=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u6267\u884cSQL\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u6267\u884cSQL\u8bed\u53e5\uff0c\u4f8b\u5982\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u3001\u67e5\u8be2\u6570\u636e\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u8868<\/p>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Bob&#39;, 25)&quot;)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u63d0\u4ea4\u4e8b\u52a1<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u9700\u8981\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528psycopg2\u8fd0\u884cSQL\u8bed\u53e5<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5psycopg2\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5psycopg2\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5bfc\u5165psycopg2\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn = psycopg2.connect(<\/p>\n<p>    dbname=&#39;yourdatabase&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    host=&#39;localhost&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u6267\u884cSQL\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u6267\u884cSQL\u8bed\u53e5\uff0c\u4f8b\u5982\u521b\u5efa\u8868\u3001\u63d2\u5165\u6570\u636e\u3001\u67e5\u8be2\u6570\u636e\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u8868<\/p>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name VARCHAR(255), age INT)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Alice&#39;, 30)&quot;)<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (&#39;Bob&#39;, 25)&quot;)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u63d0\u4ea4\u4e8b\u52a1<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u9700\u8981\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5176\u4ed6\u5e38\u89c1\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001SQL\u8bed\u6cd5\u9519\u8bef\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM non_existing_table&quot;)<\/p>\n<p>except sqlite3.Error as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u53c2\u6570\u5316\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\uff0c\u5efa\u8bae\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>cursor.execute(&quot;INSERT INTO users (name, age) VALUES (?, ?)&quot;, (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6279\u91cf\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528executemany\u65b9\u6cd5\u6279\u91cf\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">users = [(&#39;Charlie&#39;, 22), (&#39;David&#39;, 28)]<\/p>\n<p>cursor.executemany(&quot;INSERT INTO users (name, age) VALUES (?, ?)&quot;, users)<\/p>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u7b80\u5316\u8fde\u63a5\u7684\u6253\u5f00\u548c\u5173\u95ed\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with sqlite3.connect(&#39;example.db&#39;) as conn:<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>    rows = cursor.fetchall()<\/p>\n<p>    for row in rows:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u4e8b\u52a1\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u63a7\u5236\u4e8b\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;BEGIN&quot;)<\/p>\n<p>    cursor.execute(&quot;UPDATE users SET age = age + 1 WHERE name = &#39;Alice&#39;&quot;)<\/p>\n<p>    cursor.execute(&quot;UPDATE users SET age = age - 1 WHERE name = &#39;Bob&#39;&quot;)<\/p>\n<p>    conn.commit()<\/p>\n<p>except sqlite3.Error as e:<\/p>\n<p>    conn.rollback()<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u9ad8\u6548\u5730\u8fd0\u884cSQL\u8bed\u53e5\u3002\u6839\u636e\u4e0d\u540c\u7684\u6570\u636e\u5e93\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u5230\u6570\u636e\u5e93\u4ee5\u8fd0\u884cSQL\u8bed\u53e5\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fd0\u884cSQL\u8bed\u53e5\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3002\u5e38\u89c1\u7684\u6570\u636e\u5e93\u5982MySQL\u3001PostgreSQL\u548cSQLite\u90fd\u6709\u76f8\u5e94\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eMySQL\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector-python<\/code>\u5e93\u3002\u901a\u8fc7\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u5bf9\u8c61\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u8fde\u63a5\u6267\u884cSQL\u8bed\u53e5\u4e86\u3002\u8fde\u63a5\u540e\u53ef\u4ee5\u4f7f\u7528<code>cursor()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u901a\u8fc7\u6e38\u6807\u6765\u6267\u884cSQL\u8bed\u53e5\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<p><strong>\u6267\u884cSQL\u8bed\u53e5\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u6267\u884cSQL\u8bed\u53e5\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u6765\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\u3002\u901a\u8fc7\u4f7f\u7528\u5360\u4f4d\u7b26\uff08\u4f8b\u5982<code>%s<\/code>\uff09\u5e76\u5c06\u53c2\u6570\u4f20\u9012\u7ed9<code>execute()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u62a4\u6570\u636e\u5e93\u7684\u5b89\u5168\u3002\u6b64\u5916\uff0c\u68c0\u67e5SQL\u8bed\u53e5\u7684\u8bed\u6cd5\uff0c\u786e\u4fdd\u8fde\u63a5\u6570\u636e\u5e93\u7684\u7528\u6237\u6709\u8db3\u591f\u7684\u6743\u9650\u6267\u884c\u6240\u9700\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u6267\u884cSQL\u8bed\u53e5\u540e\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fd0\u884cSQL\u8bed\u53e5\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6807\u5bf9\u8c61\u7684<code>fetchone()<\/code>\u3001<code>fetchall()<\/code>\u6216<code>fetchmany(size)<\/code>\u65b9\u6cd5\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u3002<code>fetchone()<\/code>\u5c06\u8fd4\u56de\u4e00\u6761\u8bb0\u5f55\uff0c<code>fetchall()<\/code>\u5c06\u8fd4\u56de\u6240\u6709\u8bb0\u5f55\uff0c\u800c<code>fetchmany(size)<\/code>\u5c06\u8fd4\u56de\u6307\u5b9a\u6570\u91cf\u7684\u8bb0\u5f55\u3002\u5904\u7406\u5b8c\u7ed3\u679c\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6e38\u6807\u548c\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u5e76\u786e\u4fdd\u6570\u636e\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u901a\u8fc7Python\u8fd0\u884cSQL\u8bed\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\uff0c\u6700\u5e38\u89c1\u7684\u5e93\u6709\uff1asqlite3\u3001PyMySQL\u3001psyc [&hellip;]","protected":false},"author":3,"featured_media":1171905,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171899"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1171899"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171899\/revisions"}],"predecessor-version":[{"id":1171906,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171899\/revisions\/1171906"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171905"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}