{"id":1128578,"date":"2025-01-08T20:21:52","date_gmt":"2025-01-08T12:21:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128578.html"},"modified":"2025-01-08T20:21:54","modified_gmt":"2025-01-08T12:21:54","slug":"%e5%a6%82%e4%bd%95%e6%88%91%e5%88%9apython%e5%81%9a%e6%95%b0%e6%8d%ae%e5%ba%93%e8%af%be%e8%ae%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128578.html","title":{"rendered":"\u5982\u4f55\u6211\u521apython\u505a\u6570\u636e\u5e93\u8bfe\u8bbe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095230\/928f82f3-2c48-4a9c-a7fd-1901bfe43fb3.webp\" alt=\"\u5982\u4f55\u6211\u521apython\u505a\u6570\u636e\u5e93\u8bfe\u8bbe\" \/><\/p>\n<p><p> <strong>\u4e00\u3001\u5982\u4f55\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff08DBMS\uff09\uff0c\u7136\u540e\u901a\u8fc7Python\u4e0eDBMS\u8fdb\u884c\u4ea4\u4e92\u3002<strong>\u9009\u62e9\u6570\u636e\u5e93\u7cfb\u7edf\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u6570\u636e\u5e93\u3001\u8bbe\u8ba1\u6570\u636e\u5e93\u6a21\u5f0f\u3001\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u4f18\u5316\u4e0e\u6d4b\u8bd5<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e00\u6b65\u7684\u5177\u4f53\u64cd\u4f5c\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9009\u62e9\u6570\u636e\u5e93\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff08DBMS\uff09\u662f\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u7684\u9996\u8981\u6b65\u9aa4\u3002\u5e38\u89c1\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u6709\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MySQL\u3001PostgreSQL\uff09\u548c\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MongoDB\u3001Redis\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. \u5173\u7cfb\u578b\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4f7f\u7528\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff08SQL\uff09\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u548c\u64cd\u4f5c\u3002MySQL\u548cPostgreSQL\u662f\u6700\u5e38\u89c1\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>MySQL<\/strong>\uff1aMySQL\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u652f\u6301\u591a\u79cd\u5e73\u53f0\uff0c\u6613\u4e8e\u5b89\u88c5\u548c\u4f7f\u7528\u3002<\/li>\n<li><strong>PostgreSQL<\/strong>\uff1aPostgreSQL\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u652f\u6301\u590d\u6742\u67e5\u8be2\u3001\u4e8b\u52a1\u5904\u7406\u548c\u6570\u636e\u5b8c\u6574\u6027\u7ea6\u675f\u3002<\/li>\n<\/ul>\n<p><h4>2. \u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e0d\u4f7f\u7528\u4f20\u7edf\u7684\u8868\u683c\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5904\u7406\u975e\u7ed3\u6784\u5316\u6570\u636e\u3002MongoDB\u548cRedis\u662f\u5e38\u89c1\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>MongoDB<\/strong>\uff1aMongoDB\u662f\u4e00\u4e2a\u57fa\u4e8e\u6587\u6863\u7684NoSQL\u6570\u636e\u5e93\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5f00\u53d1\u548c\u7075\u6d3b\u6570\u636e\u6a21\u578b\u7684\u5e94\u7528\u3002<\/li>\n<li><strong>Redis<\/strong>\uff1aRedis\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u652f\u6301\u952e\u503c\u5bf9\u5b58\u50a8\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u548c\u914d\u7f6e\u6570\u636e\u5e93\u662f\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u7684\u57fa\u7840\u3002\u4ee5\u4e0b\u662fMySQL\u548cMongoDB\u7684\u5b89\u88c5\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. MySQL\u7684\u5b89\u88c5\u548c\u914d\u7f6e<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4e0b\u8f7d\u548c\u5b89\u88c5<\/strong>\uff1a\u4eceMySQL\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\uff0c\u5e76\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<li><strong>\u914d\u7f6e\u6570\u636e\u5e93<\/strong>\uff1a\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u6216\u56fe\u5f62\u5316\u754c\u9762\uff08\u5982MySQL Workbench\uff09\u8fdb\u884c\u6570\u636e\u5e93\u914d\u7f6e\uff0c\u5305\u62ec\u8bbe\u7f6e\u6570\u636e\u5e93\u7528\u6237\u548c\u6743\u9650\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\"># \u542f\u52a8MySQL\u670d\u52a1<\/p>\n<p>sudo service mysql start<\/p>\n<h2><strong>\u767b\u5f55MySQL<\/strong><\/h2>\n<p>mysql -u root -p<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/h2>\n<p>CREATE DATABASE my_database;<\/p>\n<h2><strong>\u521b\u5efa\u7528\u6237\u5e76\u6388\u4e88\u6743\u9650<\/strong><\/h2>\n<p>CREATE USER &#39;my_user&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;password&#39;;<\/p>\n<p>GRANT ALL PRIVILEGES ON my_database.* TO &#39;my_user&#39;@&#39;localhost&#39;;<\/p>\n<p>FLUSH PRIVILEGES;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. MongoDB\u7684\u5b89\u88c5\u548c\u914d\u7f6e<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4e0b\u8f7d\u548c\u5b89\u88c5<\/strong>\uff1a\u4eceMongoDB\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\uff0c\u5e76\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<li><strong>\u914d\u7f6e\u6570\u636e\u5e93<\/strong>\uff1a\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u542f\u52a8MongoDB\u670d\u52a1\uff0c\u5e76\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff08mongo shell\uff09\u8fdb\u884c\u6570\u636e\u5e93\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\"># \u542f\u52a8MongoDB\u670d\u52a1<\/p>\n<p>sudo service mongod start<\/p>\n<h2><strong>\u8fde\u63a5MongoDB<\/strong><\/h2>\n<p>mongo<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u548c\u96c6\u5408<\/strong><\/h2>\n<p>use my_database<\/p>\n<p>db.createCollection(&#39;my_collection&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u8ba1\u6570\u636e\u5e93\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u8bbe\u8ba1\u6570\u636e\u5e93\u6a21\u5f0f\u662f\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e00\u4e2a\u826f\u597d\u7684\u6570\u636e\u5e93\u6a21\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u5b58\u50a8\u6548\u7387\u548c\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5173\u7cfb\u578b\u6570\u636e\u5e93\u6a21\u5f0f\u8bbe\u8ba1<\/h4>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6a21\u5f0f\u65f6\uff0c\u9700\u8981\u5b9a\u4e49\u8868\u7ed3\u6784\u3001\u5b57\u6bb5\u7c7b\u578b\u548c\u5173\u7cfb\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8868\u7ed3\u6784<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u5b9a\u4e49\u6570\u636e\u5e93\u4e2d\u5404\u4e2a\u8868\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u8868\u540d\u3001\u5b57\u6bb5\u540d\u548c\u5b57\u6bb5\u7c7b\u578b\u3002<\/li>\n<li><strong>\u5b57\u6bb5\u7c7b\u578b<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u5b57\u6bb5\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u7b49\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b58\u50a8\u6548\u7387\u3002<\/li>\n<li><strong>\u5173\u7cfb<\/strong>\uff1a\u5b9a\u4e49\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5982\u4e00\u5bf9\u4e00\u3001\u4e00\u5bf9\u591a\u3001\u591a\u5bf9\u591a\u5173\u7cfb\uff0c\u5e76\u4f7f\u7528\u5916\u952e\u7ea6\u675f\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-sql\">-- \u521b\u5efa\u7528\u6237\u8868<\/p>\n<p>CREATE TABLE users (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    username VARCHAR(50) NOT NULL,<\/p>\n<p>    em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l VARCHAR(100) NOT NULL,<\/p>\n<p>    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP<\/p>\n<p>);<\/p>\n<p>-- \u521b\u5efa\u6587\u7ae0\u8868<\/p>\n<p>CREATE TABLE posts (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    user_id INT,<\/p>\n<p>    title VARCHAR(100) NOT NULL,<\/p>\n<p>    content TEXT NOT NULL,<\/p>\n<p>    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<\/p>\n<p>    FOREIGN KEY (user_id) REFERENCES users(id)<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6a21\u5f0f\u8bbe\u8ba1<\/h4>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6a21\u5f0f\u65f6\uff0c\u9700\u8981\u5b9a\u4e49\u6587\u6863\u7ed3\u6784\u548c\u96c6\u5408\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6587\u6863\u7ed3\u6784<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u5b9a\u4e49\u6bcf\u4e2a\u6587\u6863\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u5b57\u6bb5\u540d\u548c\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>\u96c6\u5408<\/strong>\uff1a\u5c06\u76f8\u5173\u7684\u6587\u6863\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u96c6\u5408\u4e2d\uff0c\u7c7b\u4f3c\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u8868\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-json\">\/\/ \u7528\u6237\u6587\u6863<\/p>\n<p>{<\/p>\n<p>    &quot;username&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john.doe@example.com&quot;,<\/p>\n<p>    &quot;created_at&quot;: ISODate(&quot;2023-10-01T00:00:00Z&quot;)<\/p>\n<p>}<\/p>\n<p>\/\/ \u6587\u7ae0\u6587\u6863<\/p>\n<p>{<\/p>\n<p>    &quot;user_id&quot;: ObjectId(&quot;60c72b2f9f1b9b8a22e8b456&quot;),<\/p>\n<p>    &quot;title&quot;: &quot;My First Post&quot;,<\/p>\n<p>    &quot;content&quot;: &quot;This is the content of my first post.&quot;,<\/p>\n<p>    &quot;created_at&quot;: ISODate(&quot;2023-10-01T00:00:00Z&quot;)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u7684\u6838\u5fc3\u90e8\u5206\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u548c\u6846\u67b6\u6765\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u3002\u5bf9\u4e8eMySQL\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector-python<\/code>\u5e93\uff1b\u5bf9\u4e8eMongoDB\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pymongo<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5MySQL\u8fde\u63a5\u5668<\/p>\n<p>pip install mysql-connector-python<\/p>\n<h2><strong>\u5b89\u88c5MongoDB\u8fde\u63a5\u5668<\/strong><\/h2>\n<p>pip install pymongo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Python\u64cd\u4f5cMySQL<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u64cd\u4f5cMySQL\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff0c\u5305\u62ec\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u548c\u63d2\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;my_user&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;my_database&#39;<\/p>\n<p>)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u7528\u6237<\/strong><\/h2>\n<p>cursor.execute(&quot;INSERT INTO users (username, email) VALUES (%s, %s)&quot;, (&#39;john_doe&#39;, &#39;john.doe@example.com&#39;))<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u7528\u6237<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>for row in cursor.fetchall():<\/p>\n<p>    print(row)<\/p>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528Python\u64cd\u4f5cMongoDB<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u64cd\u4f5cMongoDB\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff0c\u5305\u62ec\u8fde\u63a5\u6570\u636e\u5e93\u3001\u63d2\u5165\u6587\u6863\u548c\u67e5\u8be2\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pymongo import MongoClient<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>client = MongoClient(&#39;localhost&#39;, 27017)<\/p>\n<p>db = client.my_database<\/p>\n<h2><strong>\u63d2\u5165\u7528\u6237\u6587\u6863<\/strong><\/h2>\n<p>db.users.insert_one({<\/p>\n<p>    &quot;username&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john.doe@example.com&quot;,<\/p>\n<p>    &quot;created_at&quot;: &quot;2023-10-01T00:00:00Z&quot;<\/p>\n<p>})<\/p>\n<h2><strong>\u67e5\u8be2\u7528\u6237\u6587\u6863<\/strong><\/h2>\n<p>for user in db.users.find():<\/p>\n<p>    print(user)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u4e0e\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u4e0e\u6d4b\u8bd5\u662f\u786e\u4fdd\u6570\u636e\u5e93\u8bbe\u8ba1\u548c\u64cd\u4f5c\u9ad8\u6548\u53ef\u9760\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f18\u5316\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f18\u5316\u6570\u636e\u5e93\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u548c\u67e5\u8be2\u7684\u6548\u7387\u3002\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\u5305\u62ec\u7d22\u5f15\u3001\u5206\u533a\u548c\u7f13\u5b58\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7d22\u5f15<\/strong>\uff1a\u4e3a\u9891\u7e41\u67e5\u8be2\u7684\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u901f\u5ea6\u3002<\/li>\n<li><strong>\u5206\u533a<\/strong>\uff1a\u5c06\u5927\u578b\u8868\u5206\u6210\u591a\u4e2a\u8f83\u5c0f\u7684\u5206\u533a\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7ba1\u7406\u548c\u67e5\u8be2\u6548\u7387\u3002<\/li>\n<li><strong>\u7f13\u5b58<\/strong>\uff1a\u4f7f\u7528\u7f13\u5b58\u673a\u5236\uff08\u5982Redis\uff09\u5b58\u50a8\u5e38\u7528\u6570\u636e\uff0c\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u7684\u67e5\u8be2\u538b\u529b\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-sql\">-- \u4e3a\u7528\u6237\u8868\u7684\u7528\u6237\u540d\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15<\/p>\n<p>CREATE INDEX idx_username ON users(username);<\/p>\n<p>-- \u4e3a\u6587\u7ae0\u8868\u7684\u7528\u6237ID\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15<\/p>\n<p>CREATE INDEX idx_user_id ON posts(user_id);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6d4b\u8bd5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u5e93\u8bbe\u8ba1\u548c\u64cd\u4f5c\u540e\uff0c\u9700\u8981\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u6d4b\u8bd5\u5185\u5bb9\u5305\u62ec\u529f\u80fd\u6d4b\u8bd5\u3001\u6027\u80fd\u6d4b\u8bd5\u548c\u5b89\u5168\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u6d4b\u8bd5<\/strong>\uff1a\u9a8c\u8bc1\u6240\u6709\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u548c\u67e5\u8be2\u3002<\/li>\n<li><strong>\u6027\u80fd\u6d4b\u8bd5<\/strong>\uff1a\u8bc4\u4f30\u6570\u636e\u5e93\u5728\u9ad8\u8d1f\u8f7d\u4e0b\u7684\u6027\u80fd\u8868\u73b0\uff0c\u5982\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002<\/li>\n<li><strong>\u5b89\u5168\u6d4b\u8bd5<\/strong>\uff1a\u68c0\u67e5\u6570\u636e\u5e93\u662f\u5426\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\uff0c\u5982SQL\u6ce8\u5165\u548c\u672a\u6388\u6743\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u529f\u80fd\u6d4b\u8bd5\u793a\u4f8b<\/p>\n<p>def test_insert_user():<\/p>\n<p>    conn = mysql.connector.connect(<\/p>\n<p>        host=&#39;localhost&#39;,<\/p>\n<p>        user=&#39;my_user&#39;,<\/p>\n<p>        password=&#39;password&#39;,<\/p>\n<p>        database=&#39;my_database&#39;<\/p>\n<p>    )<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;INSERT INTO users (username, email) VALUES (%s, %s)&quot;, (&#39;test_user&#39;, &#39;test@example.com&#39;))<\/p>\n<p>    conn.commit()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM users WHERE username = %s&quot;, (&#39;test_user&#39;,))<\/p>\n<p>    assert cursor.fetchone() is not None<\/p>\n<p>    cursor.close()<\/p>\n<p>    conn.close()<\/p>\n<p>test_insert_user()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884cPython\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u6570\u636e\u5e93\u3001\u8bbe\u8ba1\u5408\u7406\u7684\u6570\u636e\u5e93\u6a21\u5f0f\u3001\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u4f18\u5316\u4e0e\u6d4b\u8bd5\u662f\u5173\u952e\u6b65\u9aa4\u3002\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u591f\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u6b65\u9aa4\uff0c\u5e76\u80fd\u591f\u6210\u529f\u5b8c\u6210\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\u3002\u65e0\u8bba\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8fd8\u662f\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u6765\u652f\u6301\u6211\u7684Python\u8bfe\u8bbe\uff1f<\/strong><br \/>\u5728\u9009\u62e9\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u9879\u76ee\u7684\u9700\u6c42\u548c\u89c4\u6a21\u3002\u5e38\u89c1\u7684\u9009\u62e9\u5305\u62ecSQLite\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\uff0c\u5e76\u4e14\u4e0ePython\u6709\u5f88\u597d\u7684\u517c\u5bb9\u6027\uff1bPostgreSQL\u548cMySQL\u5219\u9002\u7528\u4e8e\u4e2d\u5230\u5927\u578b\u9879\u76ee\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u8bc4\u4f30\u6570\u636e\u5e93\u7684\u5b66\u4e60\u66f2\u7ebf\u548c\u793e\u533a\u652f\u6301\u4e5f\u5f88\u91cd\u8981\uff0c\u8fd9\u4f1a\u5f71\u54cd\u5230\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u95ee\u9898\u89e3\u51b3\u6548\u7387\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\u548c\u64cd\u4f5c\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5e93\u5982<code>sqlite3<\/code>\u3001<code>SQLAlchemy<\/code>\u6216<code>psycopg2<\/code>\uff08\u9488\u5bf9PostgreSQL\uff09\u6765\u5b9e\u73b0\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u63a5\u53e3\u548c\u529f\u80fd\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e93\u80fd\u5e2e\u52a9\u7b80\u5316\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3001\u67e5\u8be2\u548c\u6570\u636e\u5904\u7406\u3002\u786e\u4fdd\u9605\u8bfb\u76f8\u5173\u6587\u6863\uff0c\u4ee5\u4e86\u89e3\u5982\u4f55\u6267\u884c\u57fa\u672c\u7684CRUD\uff08\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u548c\u5220\u9664\uff09\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u7a0b\u5e8f\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u6548\u7387\uff1f<\/strong><br \/>\u4f18\u5316\u6570\u636e\u5e93\u4ea4\u4e92\u6548\u7387\u7684\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u51cf\u5c11\u8fde\u63a5\u65f6\u95f4\uff0c\u5408\u7406\u8bbe\u8ba1\u6570\u636e\u5e93\u7d22\u5f15\u4ee5\u52a0\u901f\u67e5\u8be2\uff0c\u4ee5\u53ca\u4f7f\u7528\u6279\u91cf\u5904\u7406\u6765\u51cf\u5c11\u6570\u636e\u5e93\u4ea4\u4e92\u6b21\u6570\u3002\u6b64\u5916\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u6267\u884c\u5927\u91cf\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5c3d\u91cf\u4f7f\u7528\u4e00\u6b21\u6027\u67e5\u8be2\u548c\u66f4\u65b0\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u5982\u4f55\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1 \u8981\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u8bfe\u7a0b\u8bbe\u8ba1\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf [&hellip;]","protected":false},"author":3,"featured_media":1128581,"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\/1128578"}],"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=1128578"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128578\/revisions"}],"predecessor-version":[{"id":1128585,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128578\/revisions\/1128585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128581"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}