{"id":974702,"date":"2024-12-27T06:07:17","date_gmt":"2024-12-26T22:07:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974702.html"},"modified":"2024-12-27T06:07:19","modified_gmt":"2024-12-26T22:07:19","slug":"python%e5%a6%82%e4%bd%95%e8%af%bbmdb%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974702.html","title":{"rendered":"python\u5982\u4f55\u8bfbmdb\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200305\/c22d8952-99a9-4577-a5f9-9a8f9f22f924.webp\" alt=\"python\u5982\u4f55\u8bfbmdb\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u8bfb\u53d6MDB\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u6bd4\u5982<code>pyodbc<\/code>\u3001<code>pandas<\/code>\u914d\u5408<code>sqlalchemy<\/code>\u3001<code>pymdb<\/code>\u7b49\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff0c\u5176\u6b21\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u6700\u540e\u4f7f\u7528SQL\u67e5\u8be2\u6765\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002<\/strong>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u8bfb\u53d6MDB\u6587\u4ef6\uff0c\u5e76\u9010\u6b65\u5c55\u793a\u6bcf\u4e2a\u6b65\u9aa4\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PYODBC\u8bfb\u53d6MDB\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>pyodbc<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u5e93\uff0c\u7528\u4e8e\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u5305\u62ecMDB\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PYODBC<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528<code>pyodbc<\/code>\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p><p>\u8981\u8fde\u63a5\u5230MDB\u6587\u4ef6\uff0c\u9700\u8981\u5b89\u88c5Microsoft Access Database Engine\u3002\u8fd9\u662f\u56e0\u4e3aMDB\u6587\u4ef6\u662fMicrosoft Access\u6570\u636e\u5e93\u6587\u4ef6\u7684\u4e00\u79cd\u683c\u5f0f\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u8bfb\u53d6MDB\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5e76\u8bfb\u53d6MDB\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e\u5e93\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>mdb_file_path = &#39;path_to_your_file.mdb&#39;<\/p>\n<h2><strong>\u8fde\u63a5\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>conn_str = (<\/p>\n<p>    r&#39;DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};&#39;<\/p>\n<p>    r&#39;DBQ=&#39; + mdb_file_path + &#39;;&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u5efa\u7acb\u8fde\u63a5<\/strong><\/h2>\n<p>conn = pyodbc.connect(conn_str)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM your_table_name&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u884c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e86MDB\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528ODBC\u8fde\u63a5\u5b57\u7b26\u4e32\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u63a5\u4e0b\u6765\u901a\u8fc7SQL\u67e5\u8be2\u63d0\u53d6\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PANDAS\u548cSQLALCHEMY\u8bfb\u53d6MDB\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u7ed3\u5408<code>sqlalchemy<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6570\u636e\u5e93\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pandas sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528SQLAlchemy\u548cPandas\u8bfb\u53d6MDB\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sqlalchemy import create_engine<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e\u5e93\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>mdb_file_path = &#39;path_to_your_file.mdb&#39;<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(f&#39;access+pyodbc:\/\/\/?odbc_connect={conn_str}&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e\u5230DataFrame<\/strong><\/h2>\n<p>df = pd.read_sql(&#39;SELECT * FROM your_table_name&#39;, engine)<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7<code>create_engine<\/code>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7136\u540e\u4f7f\u7528<code>pandas.read_sql<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u8bfb\u53d6\u5230DataFrame\u4e2d\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528MDB-TOOLS\u548cPandas<\/p>\n<\/p>\n<p><p><code>mdb-tools<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8bbf\u95eeMDB\u6587\u4ef6\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u4e0e<code>pandas<\/code>\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5MDB-TOOLS<\/li>\n<\/ol>\n<p><p>\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u6709\u4e0d\u540c\u7684\u5b89\u88c5\u65b9\u6cd5\uff0c\u4f8b\u5982\u5728Linux\u4e0a\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install mdbtools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528MDB-TOOLS\u5bfc\u51fa\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>mdb-tools<\/code>\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5bfc\u51fa\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">import pandas as pd<\/p>\n<p>import subprocess<\/p>\n<h2><strong>\u5bfc\u51faMDB\u8868\u5230CSV<\/strong><\/h2>\n<p>subprocess.run([&#39;mdb-export&#39;, &#39;path_to_your_file.mdb&#39;, &#39;your_table_name&#39;, &#39;-o&#39;, &#39;output.csv&#39;])<\/p>\n<h2><strong>\u4f7f\u7528pandas\u8bfb\u53d6CSV<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;output.csv&#39;)<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u501f\u52a9<code>mdb-tools<\/code>\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u683c\u5f0f\uff0c\u7136\u540e\u901a\u8fc7<code>pandas<\/code>\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9MDB\u6587\u4ef6\u7684\u8bbf\u95ee\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528PYMDB\u8bfb\u53d6MDB\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>pymdb<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u8bfb\u53d6MDB\u6587\u4ef6\u7684Python\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PYMDB<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pymdb<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528PYMDB\u8bfb\u53d6MDB\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pymdb import open_mdb<\/p>\n<h2><strong>\u6253\u5f00MDB\u6587\u4ef6<\/strong><\/h2>\n<p>db = open_mdb(&#39;path_to_your_file.mdb&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u8868\u540d<\/strong><\/h2>\n<p>tables = db.tables()<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for table_name in tables:<\/p>\n<p>    table = db.table(table_name)<\/p>\n<p>    for row in table:<\/p>\n<p>        print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93<\/strong><\/h2>\n<p>db.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong><code>pymdb<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u6765\u8bfb\u53d6MDB\u6587\u4ef6\u4e2d\u7684\u8868\u548c\u6570\u636e\uff0c\u5bf9\u4e8e\u4e0d\u9700\u8981\u590d\u6742SQL\u67e5\u8be2\u7684\u573a\u666f\u975e\u5e38\u9002\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\u7531\u4e8eMDB\u6587\u4ef6\u662fMicrosoft Access\u683c\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6570\u636e\u5e93\u5f15\u64ce\u3002<\/li>\n<li>\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u5177\u5907\u76f8\u5e94\u7684\u6587\u4ef6\u8bbf\u95ee\u6743\u9650\u3002<\/li>\n<li>\u4e0d\u540c\u7684\u5e93\u548c\u65b9\u6cd5\u6709\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6848\u3002<\/li>\n<li>\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u6ce8\u610f\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406MDB\u6587\u4ef6\u7684\u6570\u636e\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u662f\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6MDB\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6MDB\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u6216<code>pandas<\/code>\u5e93\u7ed3\u5408<code>sqlalchemy<\/code>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\u4eec\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install pyodbc pandas sqlalchemy\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>pyodbc<\/code>\u8fde\u63a5\u5230MDB\u6587\u4ef6\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\u6765\u83b7\u53d6\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pyodbc\n\n# \u8fde\u63a5\u5230MDB\u6587\u4ef6\nconn = pyodbc.connect(r&#39;DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path_to_your_file.mdb;&#39;)\n\n# \u521b\u5efa\u6e38\u6807\u5e76\u6267\u884c\u67e5\u8be2\ncursor = conn.cursor()\ncursor.execute(&quot;SELECT * FROM your_table_name&quot;)\n\n# \u83b7\u53d6\u6240\u6709\u7ed3\u679c\nrows = cursor.fetchall()\nfor row in rows:\n    print(row)\n\n# \u5173\u95ed\u8fde\u63a5\ncursor.close()\nconn.close()\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6MDB\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport pyodbc\n\nconn = pyodbc.connect(r&#39;DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path_to_your_file.mdb;&#39;)\ndf = pd.read_sql(&quot;SELECT * FROM your_table_name&quot;, conn)\nconn.close()\n\nprint(df)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u5904\u7406MDB\u6587\u4ef6\u7684\u5176\u4ed6\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86<code>pyodbc<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pymdb<\/code>\u548c<code>msaccess<\/code>\u7b49\u5e93\u6765\u8bfb\u53d6MDB\u6587\u4ef6\u3002<code>pymdb<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406MDB\u6587\u4ef6\u7684\u5e93\uff0c\u800c<code>msaccess<\/code>\u5219\u63d0\u4f9b\u4e86\u5bf9Microsoft Access\u6570\u636e\u5e93\u7684\u652f\u6301\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u73af\u5883\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<p><strong>\u8bfb\u53d6MDB\u6587\u4ef6\u65f6\u9047\u5230\u8fde\u63a5\u95ee\u9898\u600e\u4e48\u529e\uff1f<\/strong><br \/>\u5982\u679c\u5728\u8fde\u63a5MDB\u6587\u4ef6\u65f6\u9047\u5230\u95ee\u9898\uff0c\u9996\u5148\u68c0\u67e5\u4f60\u662f\u5426\u5b89\u88c5\u4e86\u6b63\u786e\u7684Microsoft Access\u9a71\u52a8\u7a0b\u5e8f\u3002\u5982\u679c\u9a71\u52a8\u7a0b\u5e8f\u7f3a\u5931\u6216\u4e0d\u517c\u5bb9\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8fde\u63a5\u5931\u8d25\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u8def\u5f84\u6b63\u786e\u4e14\u6587\u4ef6\u6ca1\u6709\u88ab\u5176\u4ed6\u7a0b\u5e8f\u5360\u7528\u3002\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u542f\u8ba1\u7b97\u673a\u6216\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884cPython\u811a\u672c\u6765\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u8bfb\u53d6MDB\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u6bd4\u5982pyodbc\u3001pandas\u914d\u5408sqlalchemy\u3001py [&hellip;]","protected":false},"author":3,"featured_media":974717,"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\/974702"}],"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=974702"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974702\/revisions"}],"predecessor-version":[{"id":974720,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974702\/revisions\/974720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974717"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}