{"id":360,"date":"2022-01-26T14:13:04","date_gmt":"2022-01-26T06:13:04","guid":{"rendered":"http:\/\/47.98.165.52\/?p=360"},"modified":"2022-01-26T14:13:04","modified_gmt":"2022-01-26T06:13:04","slug":"%e4%bd%bf%e7%94%a8django%e6%a1%86%e6%9e%b6%e7%9a%84connection%e5%af%b9%e8%b1%a1%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e7%9a%84%e4%b8%80%e7%82%b9","status":"publish","type":"post","link":"\/?p=360","title":{"rendered":"\u4f7f\u7528django\u6846\u67b6\u7684connection\u5bf9\u8c61\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9"},"content":{"rendered":"<p>django\u5141\u8bb8\u6211\u4eec\u6267\u884c\u81ea\u5b9a\u4e49\u7684SQL\u8bed\u53e5\u3002\u9996\u5148\u9700\u8981import connection\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-python\">from django.db import connection<\/code><\/pre>\n<p>connection\u5bf9\u8c61\u5c01\u88c5\u4e86db-api\u89c4\u8303\u4e2d\u5b9a\u4e49\u7684connection\u5bf9\u8c61\u3002\u4f46\u662fdir(connection)\u4e4b\u540e\u5e76\u6ca1\u6709\u53d1\u73b0commit\u548crollback\u65b9\u6cd5\uff0c\u5982\u4f55\u63d0\u4ea4\u4e8b\u52a1\u5462\uff1f\u5f53\u6267\u884cDELETE\u3001INSERT\u3001UPDATE\u540e\uff0c\u5fc5\u987bconnection\u5bf9\u8c61\u7684commit\u65b9\u6cd5\u624d\u80fd\u5c06\u6570\u636e\u5199\u5165\u7684\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>\u539f\u56e0\u5982\u4e0b\uff1aconnection\u5bf9\u8c61\u4e2d\u5305\u542b\u53c8\u4e00\u4e2aconnection\u5bf9\u8c61\uff0c\u5b83\u624d\u662f\u771f\u6b63\u7684\u4e0e\u5b9e\u9645\u6570\u636e\u5e93\u5efa\u7acb\u7684connection\u5bf9\u8c61\u3002\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">connection.cursor().execute(&quot;INSERT INTO table1 (col) VALUES (&#039;something&#039;)&quot;)\nconnection.connection.commit()\n\ntry:\n  raise Exception(&quot;some thing goes wrong! :(&quot;)\nexcept Exception:\n  connection.connection.rollback()<\/code><\/pre>\n<p>\u80fd\u591f\u6b63\u786e\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>django\u5141\u8bb8\u6211\u4eec\u6267\u884c\u81ea\u5b9a\u4e49\u7684SQL\u8bed\u53e5\u3002\u9996\u5148\u9700\u8981import connection\u5bf9\u8c61\uff1a from django.db import   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[33],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/360"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":361,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions\/361"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}