{"id":96996,"date":"2024-10-20T14:07:18","date_gmt":"2024-10-20T06:07:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96996\/"},"modified":"2024-10-20T14:07:18","modified_gmt":"2024-10-20T06:07:18","slug":"python%e5%b8%b8%e7%94%a8%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%8b%e7%b4%b9-python-%e5%b8%b8%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96996\/","title":{"rendered":"Python\u5e38\u7528\u7684\u6578\u64da\u5eab\u4ecb\u7d39 (python \u5e38\u7528\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e5%b8%b8%e7%94%a8%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%8b%e7%b4%b9-dQuTRDposd\">Python\u5e38\u7528\u7684\u6578\u64da\u5eab\u4ecb\u7d39<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cPython\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u5206\u6790\u3001\u7db2\u9801\u958b\u767c\u548c\u81ea\u52d5\u5316\u7b49\u9818\u57df\u3002\u96a8\u8457\u61c9\u7528\u9700\u6c42\u7684\u589e\u52a0\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u6210\u70ba\u958b\u767c\u8005\u7684\u91cd\u8981\u4efb\u52d9\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5e7e\u7a2ePython\u5e38\u7528\u7684\u6578\u64da\u5eab\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u9078\u64c7\u548c\u4f7f\u7528\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"1-sqlite-dQuTRDposd\">1. SQLite<\/h2>\n<p>SQLite\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u95dc\u806f\u6578\u64da\u5eab\uff0c\u7279\u5225\u9069\u5408\u5c0f\u578b\u61c9\u7528\u548c\u5d4c\u5165\u5f0f\u7cfb\u7d71\u3002\u5b83\u7684\u7279\u9ede\u662f\u7121\u9700\u5b89\u88dd\u548c\u914d\u7f6e\uff0c\u6578\u64da\u5eab\u4ee5\u6587\u4ef6\u5f62\u5f0f\u5b58\u5132\uff0c\u4f7f\u7528\u65b9\u4fbf\u3002<\/p>\n<pre><code>import sqlite3\n\n# \u9023\u63a5\u5230SQLite\u6578\u64da\u5eab\nconn = sqlite3.connect('example.db')\n\n# \u5275\u5efa\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\ncursor = conn.cursor()\n\n# \u5275\u5efa\u8868\ncursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')\n\n# \u63d2\u5165\u6578\u64da\ncursor.execute(\"INSERT INTO users (name, age) VALUES ('Alice', 30)\")\n\n# \u63d0\u4ea4\u4e8b\u52d9\nconn.commit()\n\n# \u67e5\u8a62\u6578\u64da\ncursor.execute(\"SELECT * FROM users\")\nprint(cursor.fetchall())\n\n# \u95dc\u9589\u9023\u63a5\nconn.close()\n<\/code><\/pre>\n<h2 id=\"2-mysql-dQuTRDposd\">2. MySQL<\/h2>\n<p>MySQL\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bcWeb\u61c9\u7528\u548c\u4f01\u696d\u7d1a\u61c9\u7528\u3002Python\u53ef\u4ee5\u901a\u904e\u7b2c\u4e09\u65b9\u5eab\u5982MySQL Connector\u6216SQLAlchemy\u4f86\u8207MySQL\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\nconn = mysql.connector.connect(\n    host='localhost',\n    user='yourusername',\n    password='yourpassword',\n    database='testdb'\n)\n\n# \u5275\u5efa\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\ncursor = conn.cursor()\n\n# \u67e5\u8a62\u6578\u64da\ncursor.execute(\"SELECT * FROM users\")\nfor row in cursor.fetchall():\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\nconn.close()\n<\/code><\/pre>\n<h2 id=\"3-postgresql-dQuTRDposd\">3. PostgreSQL<\/h2>\n<p>PostgreSQL\u662f\u4e00\u7a2e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\uff0c\u652f\u6301\u8907\u96dc\u67e5\u8a62\u548c\u6578\u64da\u5b8c\u6574\u6027\u3002Python\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528psycopg2\u5eab\u4f86\u8207PostgreSQL\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<pre><code>import psycopg2\n\n# \u9023\u63a5\u5230PostgreSQL\u6578\u64da\u5eab\nconn = psycopg2.connect(\n    dbname='testdb',\n    user='yourusername',\n    password='yourpassword',\n    host='localhost'\n)\n\n# \u5275\u5efa\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\ncursor = conn.cursor()\n\n# \u67e5\u8a62\u6578\u64da\ncursor.execute(\"SELECT * FROM users\")\nfor row in cursor.fetchall():\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\nconn.close()\n<\/code><\/pre>\n<h2 id=\"4-mongodb-dQuTRDposd\">4. MongoDB<\/h2>\n<p>MongoDB\u662f\u4e00\u7a2eNoSQL\u6578\u64da\u5eab\uff0c\u9069\u5408\u8655\u7406\u5927\u898f\u6a21\u7684\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002Python\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528PyMongo\u5eab\u4f86\u8207MongoDB\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<pre><code>from pymongo import MongoClient\n\n# \u9023\u63a5\u5230MongoDB\u6578\u64da\u5eab\nclient = MongoClient('localhost', 27017)\ndb = client['testdb']\ncollection = db['users']\n\n# \u63d2\u5165\u6578\u64da\ncollection.insert_one({'name': 'Alice', 'age': 30})\n\n# \u67e5\u8a62\u6578\u64da\nfor user in collection.find():\n    print(user)\n\n# \u95dc\u9589\u9023\u63a5\nclient.close()\n<\/code><\/pre>\n<h2 id=\"5-redis-dQuTRDposd\">5. Redis<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u7de9\u5b58\u548c\u5373\u6642\u6578\u64da\u8655\u7406\u3002Python\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528redis-py\u5eab\u4f86\u8207Redis\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u6578\u64da\u5eab\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u7f6e\u9375\u503c\u5c0d\nr.set('name', 'Alice')\n\n# \u7372\u53d6\u9375\u503c\u5c0d\nprint(r.get('name').decode('utf-8'))\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-dQuTRDposd\">\u7e3d\u7d50<\/h2>\n<p>\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u5c0d\u65bcPython\u958b\u767c\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u8f15\u91cf\u7d1a\u7684SQLite\uff0c\u9084\u662f\u529f\u80fd\u5f37\u5927\u7684PostgreSQL\uff0c\u6216\u662f\u9748\u6d3b\u7684MongoDB\uff0c\u6bcf\u7a2e\u6578\u64da\u5eab\u90fd\u6709\u5176\u7279\u5b9a\u7684\u61c9\u7528\u5834\u666f\u3002\u4e86\u89e3\u9019\u4e9b\u6578\u64da\u5eab\u7684\u7279\u9ede\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u9805\u76ee\u4e2d\u505a\u51fa\u66f4\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Python\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python\u5e38\u7528\u7684\u6578\u64da\u5eab\uff0c\u5305\u62ecSQLite\u3001PostgreSQL\u3001MySQL\u7b49\uff0c\u4e86\u89e3\u5176\u7279\u9ede\u548c\u61c9\u7528\u5834\u666f\uff0c\u52a9\u529b\u6578\u64da\u7ba1\u7406\u8207\u958b\u767c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96996","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96996"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96996\/revisions"}],"predecessor-version":[{"id":96997,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96996\/revisions\/96997"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}