{"id":96966,"date":"2024-10-20T13:51:18","date_gmt":"2024-10-20T05:51:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96966\/"},"modified":"2024-10-20T13:51:19","modified_gmt":"2024-10-20T05:51:19","slug":"python-2-7%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5%e6%8a%80%e5%b7%a7-python-2-7%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96966\/","title":{"rendered":"Python 2.7\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u6280\u5de7 (python 2.7\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python-2-7%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5%e6%8a%80%e5%b7%a7-xgkgYIAOlq\">Python 2.7\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u6280\u5de7<\/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\u96d6\u7136Python 2.7\u5df2\u65bc2020\u5e741\u6708\u505c\u6b62\u652f\u6301\uff0c\u4f46\u4ecd\u6709\u8a31\u591a\u820a\u7cfb\u7d71\u548c\u61c9\u7528\u7a0b\u5e8f\u4f9d\u8cf4\u65bc\u9019\u4e00\u7248\u672c\u3002\u672c\u6587\u5c07\u63a2\u8a0ePython 2.7\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a1%9e%e5%9e%8b-xgkgYIAOlq\">\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b<\/h2>\n<p>\u5728\u4f7f\u7528Python 2.7\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff1a<\/strong>\u5982MySQL\u3001PostgreSQL\u548cSQLite\uff0c\u9019\u4e9b\u6578\u64da\u5eab\u4f7f\u7528\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff08SQL\uff09\u4f86\u7ba1\u7406\u6578\u64da\u3002<\/li>\n<li><strong>NoSQL\u6578\u64da\u5eab\uff1a<\/strong>\u5982MongoDB\u548cCassandra\uff0c\u9019\u4e9b\u6578\u64da\u5eab\u9069\u5408\u8655\u7406\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e9%97%9c%e4%bf%82%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab-xgkgYIAOlq\">\u9023\u63a5\u95dc\u4fc2\u578b\u6578\u64da\u5eab<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python 2.7\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dmysql-connector-xgkgYIAOlq\">\u5b89\u88ddMySQL Connector<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88ddMySQL Connector\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install MySQL-python<\/code><\/pre>\n<h3 id=\"%e5%bb%ba%e7%ab%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-xgkgYIAOlq\">\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5efa\u7acb\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\uff1a<\/p>\n<pre><code>import MySQLdb\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = MySQLdb.connect(host=\"localhost\",    # \u4e3b\u6a5f\u5730\u5740\n                     user=\"yourusername\", # \u7528\u6236\u540d\n                     passwd=\"yourpassword\", # \u5bc6\u78bc\n                     db=\"yourdatabase\")    # \u6578\u64da\u5eab\u540d\u7a31\n\n# \u5275\u5efa\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\ncursor = db.cursor()\n<\/code><\/pre>\n<h3 id=\"%e5%9f%b7%e8%a1%8csql%e6%9f%a5%e8%a9%a2-xgkgYIAOlq\">\u57f7\u884cSQL\u67e5\u8a62<\/h3>\n<p>\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6e38\u6a19\u5c0d\u8c61\u57f7\u884cSQL\u67e5\u8a62\u3002\u4f8b\u5982\uff0c\u67e5\u8a62\u6578\u64da\u8868\u4e2d\u7684\u6240\u6709\u8a18\u9304\uff1a<\/p>\n<pre><code>cursor.execute(\"SELECT * FROM yourtable\")\n\n# \u7372\u53d6\u6240\u6709\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<h3 id=\"%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-xgkgYIAOlq\">\u95dc\u9589\u9023\u63a5<\/h3>\n<p>\u5b8c\u6210\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6e38\u6a19\u548c\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>cursor.close()\ndb.close()\n<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5nosql%e6%95%b8%e6%93%9a%e5%ba%ab-xgkgYIAOlq\">\u9023\u63a5NoSQL\u6578\u64da\u5eab<\/h2>\n<p>\u5c0d\u65bcNoSQL\u6578\u64da\u5eab\uff0c\u5982MongoDB\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\u9023\u63a5\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dpymongo-xgkgYIAOlq\">\u5b89\u88ddPyMongo<\/h3>\n<p>\u9996\u5148\uff0c\u5b89\u88ddPyMongo\u5eab\uff1a<\/p>\n<pre><code>pip install pymongo<\/code><\/pre>\n<h3 id=\"%e5%bb%ba%e7%ab%8bmongodb%e9%80%a3%e6%8e%a5-xgkgYIAOlq\">\u5efa\u7acbMongoDB\u9023\u63a5<\/h3>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230MongoDB\uff1a<\/p>\n<pre><code>from pymongo import MongoClient\n\n# \u9023\u63a5\u5230MongoDB\nclient = MongoClient('localhost', 27017)\n\n# \u9078\u64c7\u6578\u64da\u5eab\ndb = client.yourdatabase\n<\/code><\/pre>\n<h3 id=\"%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-xgkgYIAOlq\">\u57f7\u884c\u67e5\u8a62<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u67e5\u8a62\u6578\u64da\uff1a<\/p>\n<pre><code>collection = db.yourcollection\nresults = collection.find()\n\nfor document in results:\n    print(document)\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xgkgYIAOlq\">\u7e3d\u7d50<\/h2>\n<p>Python 2.7\u96d6\u7136\u5df2\u7d93\u904e\u6642\uff0c\u4f46\u4ecd\u7136\u5728\u8a31\u591a\u820a\u7cfb\u7d71\u4e2d\u767c\u63ee\u8457\u91cd\u8981\u4f5c\u7528\u3002\u901a\u904e\u638c\u63e1\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u6280\u5de7\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u7121\u8ad6\u662f\u4f7f\u7528\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u9084\u662fNoSQL\u6578\u64da\u5eab\uff0cPython 2.7\u90fd\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\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\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python 2.7\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-96966","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96966","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=96966"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96966\/revisions"}],"predecessor-version":[{"id":96967,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96966\/revisions\/96967"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}