{"id":948006,"date":"2024-12-26T23:59:37","date_gmt":"2024-12-26T15:59:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948006.html"},"modified":"2024-12-26T23:59:39","modified_gmt":"2024-12-26T15:59:39","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5access","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/948006.html","title":{"rendered":"python\u5982\u4f55\u8fde\u63a5access"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083544\/11a4cd31-ac0c-4150-9ae1-f9e2721d237b.webp\" alt=\"python\u5982\u4f55\u8fde\u63a5access\" \/><\/p>\n<p><p> <strong>Python\u8fde\u63a5Access\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u3001<code>pypyodbc<\/code>\u5e93\u3001<code>msaccessdb<\/code>\u5e93\u3002\u5176\u4e2d\uff0c<code>pyodbc<\/code>\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5408\u9002\u7684ODBC\u9a71\u52a8\u6765\u5b9e\u73b0\u8fde\u63a5\u3002<\/strong>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u8fde\u63a5Access\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8fde\u63a5\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u548cAccess\u7684ODBC\u9a71\u52a8\u3002\u5e38\u7528\u7684ODBC\u9a71\u52a8\u662f<code>Microsoft Access Driver (*.mdb, *.accdb)<\/code>\uff0c\u800cPython\u4e2d\u5e38\u7528\u7684\u8fde\u63a5\u5e93\u662f<code>pyodbc<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pyodbc\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5<code>pyodbc<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyodbc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Access ODBC\u9a71\u52a8<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Microsoft Access Database Engine\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5fae\u8f6f\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u914d\u7f6eODBC\u6570\u636e\u6e90<\/h3>\n<\/p>\n<p><p>\u5728\u7cfb\u7edf\u4e2d\u914d\u7f6eODBC\u6570\u636e\u6e90\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u6570\u636e\u5e93\u8fde\u63a5\u7684\u914d\u7f6e\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00ODBC\u6570\u636e\u6e90\u7ba1\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u201cODBC\u201d\u627e\u5230ODBC\u6570\u636e\u6e90\u7ba1\u7406\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u65b0\u7684\u6570\u636e\u6e90<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9009\u62e9\u201c\u6dfb\u52a0\u201d\u6309\u94ae\u3002<\/li>\n<li>\u9009\u62e9\u201cMicrosoft Access Driver (*.mdb, *.accdb)\u201d\u3002<\/li>\n<li>\u8f93\u5165\u6570\u636e\u6e90\u540d\u79f0\uff08DSN\uff09\u548c\u6570\u636e\u5e93\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pyodbc\u8fde\u63a5Access\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u8fde\u63a5Access\u6570\u636e\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyodbc<\/p>\n<h2><strong>\u8fde\u63a5\u5230Access\u6570\u636e\u5e93<\/strong><\/h2>\n<p>def connect_to_access_db(db_path):<\/p>\n<p>    try:<\/p>\n<p>        # \u6784\u9020\u8fde\u63a5\u5b57\u7b26\u4e32<\/p>\n<p>        conn_str = (<\/p>\n<p>            r&#39;DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};&#39;<\/p>\n<p>            rf&#39;DBQ={db_path};&#39;<\/p>\n<p>        )<\/p>\n<p>        # \u5efa\u7acb\u8fde\u63a5<\/p>\n<p>        connection = pyodbc.connect(conn_str)<\/p>\n<p>        print(&quot;\u6210\u529f\u8fde\u63a5\u5230\u6570\u636e\u5e93&quot;)<\/p>\n<p>        return connection<\/p>\n<p>    except pyodbc.Error as e:<\/p>\n<p>        print(&quot;\u8fde\u63a5\u5931\u8d25: &quot;, e)<\/p>\n<p>        return None<\/p>\n<h2><strong>\u4f7f\u7528\u8fde\u63a5<\/strong><\/h2>\n<p>db_path = &#39;C:\\\\path\\\\to\\\\your\\\\database.accdb&#39;<\/p>\n<p>conn = connect_to_access_db(db_path)<\/p>\n<p>if conn:<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM your_table_name&quot;)<\/p>\n<p>    for row in cursor.fetchall():<\/p>\n<p>        print(row)<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bbf\u95ee\u4e0e\u64cd\u4f5c\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u8fde\u63a5\u6210\u529f\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u8bed\u53e5\u8fdb\u884c\u6570\u636e\u7684\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u6267\u884cSQL\u67e5\u8be2\uff0c\u5e76\u901a\u8fc7<code>fetchall()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM TableName&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u6267\u884c\u63d2\u5165\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;INSERT INTO TableName (Column1, Column2) VALUES (?, ?)&quot;, (value1, value2))<\/p>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u6267\u884c\u66f4\u65b0\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;UPDATE TableName SET Column1 = ? WHERE ConditionColumn = ?&quot;, (newValue, conditionValue))<\/p>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u6267\u884c\u5220\u9664\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;DELETE FROM TableName WHERE ConditionColumn = ?&quot;, (conditionValue,))<\/p>\n<p>conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5904\u7406\u9519\u8bef\u4e0e\u4f18\u5316\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5c24\u5176\u662f\u5728SQL\u8bed\u53e5\u4e2d\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u53ef\u80fd\u7684\u6570\u636e\u5e93\u8fde\u63a5\u548c\u64cd\u4f5c\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM TableName&quot;)<\/p>\n<p>except pyodbc.Error as e:<\/p>\n<p>    print(&quot;\u6570\u636e\u5e93\u64cd\u4f5c\u9519\u8bef: &quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u6027\u80fd<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6279\u91cf\u63d2\u5165\u548c\u66f4\u65b0\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u7d22\u5f15\u4f18\u5316<\/strong>\uff1a\u5728\u6570\u636e\u5e93\u8868\u4e2d\u4f7f\u7528\u7d22\u5f15\u6765\u52a0\u901f\u67e5\u8be2\u3002<\/li>\n<li><strong>\u8fde\u63a5\u6c60<\/strong>\uff1a\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e2d\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5Access\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u914d\u7f6eODBC\u6570\u636e\u6e90\u3001\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u8fde\u63a5\u6570\u636e\u5e93\u4ee5\u53ca\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u60a8\u987a\u5229\u5b9e\u73b0Python\u4e0eAccess\u6570\u636e\u5e93\u7684\u8fde\u63a5\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0Access\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5Access\u6570\u636e\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pyodbc<\/code>\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u6b64\u5e93\u4ee5\u53caMicrosoft Access\u6570\u636e\u5e93\u5f15\u64ce\u3002\u8fde\u63a5\u5b57\u7b26\u4e32\u901a\u5e38\u5305\u62ec\u6587\u4ef6\u8def\u5f84\u3001\u9a71\u52a8\u7a0b\u5e8f\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import pyodbc\n\naccess_db_file = r&#39;path\\to\\your\\database.accdb&#39;\nconnection_string = f&#39;DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={access_db_file};&#39;\nconnection = pyodbc.connect(connection_string)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u6839\u636e\u60a8\u7684\u7cfb\u7edf\u73af\u5883\u548cAccess\u7248\u672c\u9009\u62e9\u6b63\u786e\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bbf\u95eeAccess\u6570\u636e\u5e93\u9700\u8981\u54ea\u4e9b\u524d\u7f6e\u6761\u4ef6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8bbf\u95eeAccess\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5<code>pyodbc<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pyodbc<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86Microsoft Access\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528<code>.accdb<\/code>\u6587\u4ef6\u65f6\u3002\u6839\u636e\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u548cAccess\u7248\u672c\u9009\u62e9\u5408\u9002\u7684\u9a71\u52a8\u7a0b\u5e8f\u4e5f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8fde\u63a5\u5230Access\u6570\u636e\u5e93\u540e\u6267\u884cSQL\u67e5\u8be2\uff1f<\/strong><br \/>\u8fde\u63a5\u5230Access\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cursor<\/code>\u5bf9\u8c61\u6267\u884cSQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">cursor = connection.cursor()\ncursor.execute(&quot;SELECT * FROM your_table_name&quot;)\nrows = cursor.fetchall()\n\nfor row in rows:\n    print(row)\n<\/code><\/pre>\n<p>\u901a\u8fc7<code>fetchall()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>fetchone()<\/code>\u83b7\u53d6\u5355\u884c\u6570\u636e\u3002\u8bb0\u5f97\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fde\u63a5Access\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pyodbc\u5e93\u3001pypyodbc\u5e93\u3001msaccessdb\u5e93\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":948015,"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\/948006"}],"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=948006"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948006\/revisions"}],"predecessor-version":[{"id":948017,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948006\/revisions\/948017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948015"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}