{"id":96990,"date":"2024-10-20T14:04:18","date_gmt":"2024-10-20T06:04:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96990\/"},"modified":"2024-10-20T14:04:19","modified_gmt":"2024-10-20T06:04:19","slug":"python%e7%b7%a8%e7%a8%8b%e5%88%a9%e5%99%a8%ef%bc%9acx_oracle%e9%80%a3%e6%8e%a5oracle%e6%95%b8%e6%93%9a%e5%ba%ab-cx_oracle%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96990\/","title":{"rendered":"Python\u7de8\u7a0b\u5229\u5668\uff1acx_Oracle\u9023\u63a5Oracle\u6578\u64da\u5eab (cx_oracle\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e7%b7%a8%e7%a8%8b%e5%88%a9%e5%99%a8%ef%bc%9acx_oracle%e9%80%a3%e6%8e%a5oracle%e6%95%b8%e6%93%9a%e5%ba%ab-aWAjfrFOTd\">Python\u7de8\u7a0b\u5229\u5668\uff1acx_Oracle\u9023\u63a5Oracle\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5df2\u7d93\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u800c\u5728Python\u4e2d\uff0ccx_Oracle\u662f\u4e00\u500b\u5f37\u5927\u7684\u5eab\uff0c\u5c08\u9580\u7528\u65bc\u9023\u63a5\u548c\u64cd\u4f5cOracle\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ecx_Oracle\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u512a\u52e2\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afcx_oracle%ef%bc%9f-aWAjfrFOTd\">\u4ec0\u9ebc\u662fcx_Oracle\uff1f<\/h2>\n<p>cx_Oracle\u662f\u4e00\u500bPython\u5eab\uff0c\u5141\u8a31\u958b\u767c\u8005\u8207Oracle\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684API\u4f86\u57f7\u884cSQL\u67e5\u8a62\u3001\u63d2\u5165\u6578\u64da\u3001\u66f4\u65b0\u6578\u64da\u4ee5\u53ca\u8655\u7406\u4e8b\u52d9\u3002cx_Oracle\u652f\u6301Oracle\u7684\u591a\u7a2e\u7279\u6027\uff0c\u5305\u62ecPL\/SQL\u3001LOB\uff08\u5927\u5c0d\u8c61\uff09\u548cOracle\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dcx_oracle-aWAjfrFOTd\">\u5b89\u88ddcx_Oracle<\/h2>\n<p>\u5728\u4f7f\u7528cx_Oracle\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u8a72\u5eab\u3002\u53ef\u4ee5\u901a\u904epip\u547d\u4ee4\u8f15\u9b06\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install cx_Oracle<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u9700\u8981\u5b89\u88ddOracle Instant Client\uff0c\u9019\u662fcx_Oracle\u8207Oracle\u6578\u64da\u5eab\u901a\u4fe1\u6240\u5fc5\u9700\u7684\u3002\u53ef\u4ee5\u5f9eOracle\u7684\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09\u4e26\u5b89\u88dd\u76f8\u61c9\u7248\u672c\u7684Instant Client\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5oracle%e6%95%b8%e6%93%9a%e5%ba%ab-aWAjfrFOTd\">\u9023\u63a5Oracle\u6578\u64da\u5eab<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230Oracle\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import cx_Oracle\n\n# \u8a2d\u5b9a\u6578\u64da\u5eab\u9023\u63a5\u53c3\u6578\ndsn = cx_Oracle.makedsn(\"\u4e3b\u6a5f\u540d\", \"\u7aef\u53e3\", service_name=\"\u670d\u52d9\u540d\")\nconnection = cx_Oracle.connect(user=\"\u7528\u6236\u540d\", password=\"\u5bc6\u78bc\", dsn=dsn)\n\n# \u78ba\u8a8d\u9023\u63a5\nprint(\"\u6210\u529f\u9023\u63a5\u5230Oracle\u6578\u64da\u5eab\")\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u8acb\u5c07\u201c\u4e3b\u6a5f\u540d\u201d\u3001\u201c\u7aef\u53e3\u201d\u3001\u201c\u670d\u52d9\u540d\u201d\u3001\u201c\u7528\u6236\u540d\u201d\u548c\u201c\u5bc6\u78bc\u201d\u66ff\u63db\u70ba\u5be6\u969b\u7684\u6578\u64da\u5eab\u9023\u63a5\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e5%9f%b7%e8%a1%8csql%e6%9f%a5%e8%a9%a2-aWAjfrFOTd\">\u57f7\u884cSQL\u67e5\u8a62<\/h2>\n<p>\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528cursor\u5c0d\u8c61\u4f86\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u67e5\u8a62\u793a\u4f8b\uff1a<\/p>\n<pre><code>cursor = connection.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM \u8868\u540d\")\n\n# \u7372\u53d6\u67e5\u8a62\u7d50\u679c\nfor row in cursor:\n    print(row)\n\n# \u95dc\u9589cursor\ncursor.close()\n<\/code><\/pre>\n<h2 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-aWAjfrFOTd\">\u63d2\u5165\u6578\u64da<\/h2>\n<p>\u9664\u4e86\u67e5\u8a62\u6578\u64da\uff0ccx_Oracle\u9084\u652f\u6301\u63d2\u5165\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>cursor = connection.cursor()\n\n# \u63d2\u5165\u6578\u64da\ncursor.execute(\"INSERT INTO \u8868\u540d (\u52171, \u52172) VALUES (:1, :2)\", (\u503c1, \u503c2))\n\n# \u63d0\u4ea4\u4e8b\u52d9\nconnection.commit()\n\n# \u95dc\u9589cursor\ncursor.close()\n<\/code><\/pre>\n<h2 id=\"%e8%99%95%e7%90%86%e4%ba%8b%e5%8b%99-aWAjfrFOTd\">\u8655\u7406\u4e8b\u52d9<\/h2>\n<p>\u5728cx_Oracle\u4e2d\uff0c\u4e8b\u52d9\u7684\u8655\u7406\u975e\u5e38\u7c21\u55ae\u3002\u53ef\u4ee5\u4f7f\u7528commit()\u65b9\u6cd5\u63d0\u4ea4\u4e8b\u52d9\uff0c\u4f7f\u7528rollback()\u65b9\u6cd5\u56de\u6efe\u4e8b\u52d9\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<pre><code>try:\n    cursor = connection.cursor()\n    cursor.execute(\"INSERT INTO \u8868\u540d (\u52171) VALUES (:1)\", (\u503c,))\n    connection.commit()  # \u63d0\u4ea4\u4e8b\u52d9\nexcept Exception as e:\n    connection.rollback()  # \u56de\u6efe\u4e8b\u52d9\n    print(\"\u767c\u751f\u932f\u8aa4\uff1a\", e)\nfinally:\n    cursor.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-aWAjfrFOTd\">\u7e3d\u7d50<\/h2>\n<p>cx_Oracle\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684Python\u5eab\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u9023\u63a5\u548c\u64cd\u4f5cOracle\u6578\u64da\u5eab\u3002\u7121\u8ad6\u662f\u67e5\u8a62\u3001\u63d2\u5165\u9084\u662f\u8655\u7406\u4e8b\u52d9\uff0ccx_Oracle\u90fd\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5c0d\u65bc\u9700\u8981\u5728Python\u4e2d\u8207Oracle\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0ccx_Oracle\u7121\u7591\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\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\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22cx_Oracle\uff0cPython\u7de8\u7a0b\u4e2d\u9023\u63a5Oracle\u6578\u64da\u5eab\u7684\u5f37\u5927\u5de5\u5177\uff0c\u7c21\u5316\u6578\u64da\u64cd\u4f5c\u8207\u7ba1\u7406\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-96990","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96990","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=96990"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96990\/revisions"}],"predecessor-version":[{"id":96991,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96990\/revisions\/96991"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}