{"id":96986,"date":"2024-10-20T14:02:19","date_gmt":"2024-10-20T06:02:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96986\/"},"modified":"2024-10-20T14:02:19","modified_gmt":"2024-10-20T06:02:19","slug":"pythonmssql%e6%95%b8%e6%93%9a%e6%8c%96%e6%8e%98%ef%bc%9a%e5%be%9emssql%e5%a4%96%e5%bb%b6%e5%88%b0python%ef%bc%88mssqlinto%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96986\/","title":{"rendered":"pythonMSSQL\u6578\u64da\u6316\u6398\uff1a\u5f9eMSSQL\u5916\u5ef6\u5230Python\uff08mssqlinto\uff09"},"content":{"rendered":"<h1 id=\"python-mssql%e6%95%b8%e6%93%9a%e6%8c%96%e6%8e%98%ef%bc%9a%e5%be%9emssql%e5%a4%96%e5%bb%b6%e5%88%b0python%ef%bc%88mssqlinto%ef%bc%89-hPYbLqSoiT\">Python MSSQL\u6578\u64da\u6316\u6398\uff1a\u5f9eMSSQL\u5916\u5ef6\u5230Python\uff08mssqlinto\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u6578\u64da\u6316\u6398\u6210\u70ba\u4e86\u4f01\u696d\u6c7a\u7b56\u7684\u91cd\u8981\u5de5\u5177\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u5206\u6790\u548c\u6316\u6398\u9818\u57df\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Python\u5f9eMSSQL\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u4e26\u9032\u884c\u9032\u4e00\u6b65\u7684\u5206\u6790\u548c\u6316\u6398\u3002<\/p>\n<h2 id=\"mssql%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a6%82%e8%bf%b0-hPYbLqSoiT\">MSSQL\u6578\u64da\u5eab\u6982\u8ff0<\/h2>\n<p>Microsoft SQL Server\uff08MSSQL\uff09\u662f\u4e00\u7a2e\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u4f01\u696d\u74b0\u5883\u4e2d\u3002\u5b83\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u5b58\u5132\u3001\u6aa2\u7d22\u548c\u7ba1\u7406\u529f\u80fd\u3002MSSQL\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\u548c\u8907\u96dc\u7684\u67e5\u8a62\u8a9e\u8a00\uff0c\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%8c%96%e6%8e%98%ef%bc%9f-hPYbLqSoiT\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python\u9032\u884c\u6578\u64da\u6316\u6398\uff1f<\/h2>\n<p>Python\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\u800c\u53d7\u5230\u6578\u64da\u79d1\u5b78\u5bb6\u7684\u9752\u775e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u6316\u6398\u7684\u4e00\u4e9b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u6613\u65bc\u5b78\u7fd2\uff1a<\/strong>Python\u7684\u8a9e\u6cd5\u7c21\u55ae\uff0c\u9069\u5408\u521d\u5b78\u8005\u3002<\/li>\n<li><strong>\u8c50\u5bcc\u7684\u5eab\uff1a<\/strong>\u5982Pandas\u3001NumPy\u3001Matplotlib\u7b49\uff0c\u9019\u4e9b\u5eab\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u548c\u53ef\u8996\u5316\u529f\u80fd\u3002<\/li>\n<li><strong>\u793e\u5340\u652f\u6301\uff1a<\/strong>Python\u64c1\u6709\u6d3b\u8e8d\u7684\u958b\u767c\u8005\u793e\u5340\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u8cc7\u6e90\u548c\u652f\u6301\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%be%9emssql%e6%8f%90%e5%8f%96%e6%95%b8%e6%93%9a%e5%88%b0python-hPYbLqSoiT\">\u5f9eMSSQL\u63d0\u53d6\u6578\u64da\u5230Python<\/h2>\n<p>\u8981\u5f9eMSSQL\u6578\u64da\u5eab\u63d0\u53d6\u6578\u64da\u5230Python\uff0c\u901a\u5e38\u4f7f\u7528`pyodbc`\u6216`pymssql`\u9019\u5169\u500b\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528`pyodbc`\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dpyodbc-hPYbLqSoiT\">\u5b89\u88ddpyodbc<\/h3>\n<pre><code>pip install pyodbc<\/code><\/pre>\n<h3 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mssql%e6%95%b8%e6%93%9a%e5%ba%ab-hPYbLqSoiT\">\u9023\u63a5\u5230MSSQL\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5efa\u7acb\u8207MSSQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import pyodbc\n\n# \u8a2d\u5b9a\u9023\u63a5\u53c3\u6578\nserver = 'your_server'\ndatabase = 'your_database'\nusername = 'your_username'\npassword = 'your_password'\n\n# \u5efa\u7acb\u9023\u63a5\nconn = pyodbc.connect(f'DRIVER={{SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password}')\ncursor = conn.cursor()\n<\/code><\/pre>\n<h3 id=\"%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2%e4%b8%a6%e6%8f%90%e5%8f%96%e6%95%b8%e6%93%9a-hPYbLqSoiT\">\u57f7\u884c\u67e5\u8a62\u4e26\u63d0\u53d6\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u9023\u63a5\u6210\u529f\uff0c\u5c31\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u4e26\u63d0\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code># \u57f7\u884c\u67e5\u8a62\ncursor.execute('SELECT * FROM your_table')\n\n# \u63d0\u53d6\u6578\u64da\nrows = cursor.fetchall()\n\n# \u986f\u793a\u6578\u64da\nfor row in rows:\n    print(row)\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e8%88%87%e5%88%86%e6%9e%90-hPYbLqSoiT\">\u6578\u64da\u8655\u7406\u8207\u5206\u6790<\/h2>\n<p>\u63d0\u53d6\u6578\u64da\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u9032\u884c\u6578\u64da\u8655\u7406\u548c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u8f49\u63db\u70baPandas DataFrame\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import pandas as pd\n\n# \u5c07\u6578\u64da\u8f49\u63db\u70baDataFrame\ndf = pd.DataFrame.from_records(rows, columns=[column[0] for column in cursor.description])\n\n# \u986f\u793aDataFrame\nprint(df.head())\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%8f%af%e8%a6%96%e5%8c%96-hPYbLqSoiT\">\u6578\u64da\u53ef\u8996\u5316<\/h2>\n<p>\u6578\u64da\u53ef\u8996\u5316\u662f\u6578\u64da\u5206\u6790\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u7b49\u5eab\u4f86\u9032\u884c\u53ef\u8996\u5316\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import matplotlib.pyplot as plt\n\n# \u7e6a\u88fd\u5716\u8868\ndf['your_column'].value_counts().plot(kind='bar')\nplt.title('Your Title')\nplt.xlabel('X-axis Label')\nplt.ylabel('Y-axis Label')\nplt.show()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-hPYbLqSoiT\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07MSSQL\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u63d0\u53d6\u5230Python\u4e2d\uff0c\u4e26\u9032\u884c\u9032\u4e00\u6b65\u7684\u5206\u6790\u548c\u53ef\u8996\u5316\u3002\u9019\u4e00\u904e\u7a0b\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u4e5f\u70ba\u4f01\u696d\u63d0\u4f9b\u4e86\u66f4\u6df1\u5165\u7684\u6578\u64da\u6d1e\u5bdf\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u4f01\u696d\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528pythonMSSQL\u6578\u64da\u6316\u6398\u5de5\u5177mssqlinto\uff0c\u8f15\u9b06\u5c07MSSQL\u6578\u64da\u8f49\u79fb\u5230Python\u9032\u884c\u6df1\u5165\u5206\u6790\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-96986","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96986","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=96986"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96986\/revisions"}],"predecessor-version":[{"id":96987,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96986\/revisions\/96987"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}