{"id":7317,"date":"2019-08-05T22:25:06","date_gmt":"2019-08-05T13:25:06","guid":{"rendered":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=7317"},"modified":"2019-08-06T21:21:34","modified_gmt":"2019-08-06T12:21:34","slug":"pythonmysql-%e3%81%a7%e3%82%b3%e3%83%8d%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%84%e3%82%ab%e3%83%bc%e3%82%bd%e3%83%ab%e3%81%a8-with-%e6%96%87","status":"publish","type":"post","link":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=7317","title":{"rendered":"Python+MySQL \u3067\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3084\u30ab\u30fc\u30bd\u30eb\u3068 with \u6587"},"content":{"rendered":"<p>\u307e\u305f Python+MySQL \u30cd\u30bf\u3002<\/p>\n<p>\u30ab\u30fc\u30bd\u30eb\u3092\u4f7f\u3063\u305f\u3089\u9589\u3058\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u3002\u51e6\u7406\u5f8c\u306b\u305d\u306e\u6d41\u308c\u3067\u305d\u306e\u307e\u307e cursor.close() \u3092\u547c\u3076\u3088\u3046\u306b\u66f8\u304f\u3068\u4f8b\u5916\u304c\u98db\u3093\u3060\u308a\u9577\u3044\u51e6\u7406\u306e\u9014\u4e2d\u3067\u3046\u3063\u304b\u308a return \u3057\u305f\u308a\u3057\u305f\u3068\u304d\u306b\u547c\u3073\u6f0f\u308c\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3001try\uff5efinally \u306e\u3088\u3046\u306a\u69cb\u9020\u3092\u4f7f\u3044\u305f\u3044\u3068\u3053\u308d\u3067\u3059\u3002\uff08Java \u3067\u3082\u305f\u3076\u3093\u305d\u3046\uff0820\u5e74\u524d\u306e\u77e5\u8b58\uff09\u3002C++ \u3060\u3068\u30c7\u30b9\u30c8\u30e9\u30af\u30bf\u304c\u4f7f\u3048\u308b\u306e\u3067\u9055\u3046\u66f8\u304d\u65b9\u306b\u306a\u308b\uff09<\/p>\n<p><!--more--><\/p>\n<blockquote>\n<pre>\r\ncursor = conn.cursor()\r\ntry:\r\n    cursor.execute(\"SELECT * FROM table\")\r\n    rows = cursor.fetchall()\r\n    ...\r\nfinally:\r\n    cursor.close()\r\n<\/pre>\n<\/blockquote>\n<p>\u3068\u3053\u308d\u3067\u3001Python \u306b\u306f with \u6587\u3068\u3044\u3046\u306e\u304c\u3042\u3063\u3066\u3001 with \u6587\u306e\u4e2d\u306e\u30d6\u30ed\u30c3\u30af\u3092\uff08\u629c\u3051\u65b9\u306b\u304b\u304b\u308f\u3089\u305a\uff09\u629c\u3051\u305f\u3068\u304d\u306b\u4e00\u5b9a\u306e\u7d42\u4e86\u51e6\u7406\u3092\u81ea\u52d5\u3067\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002PyMySQL \u3068 mysqlclient \u3067\u306f\u30ab\u30fc\u30bd\u30eb\u306b\u5bfe\u3057\u3066\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\nwith conn.cursor() as cursor:\r\n    cursor.execute(\"SELECT * FROM table\")\r\n    rows = cursor.fetchall()\r\n    ...\r\n    # with \u304b\u3089\u629c\u3051\u308b\u3068\u304d\u306b cursor.close() \u304c\u81ea\u52d5\u7684\u306b\u547c\u3070\u308c\u308b\r\n<\/pre>\n<\/blockquote>\n<p>\u6b8b\u5ff5\u306a\u3053\u3068\u306b MySQL Connector\/Python \u3060\u3068\u3053\u306e\u66f8\u5f0f\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002with\u6587\u306f with \u306e\u5f8c\u306b\u6765\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e __enter__ \u3092\u547c\u3093\u3067 with \u3092\u629c\u3051\u308b\u3068\u304d\u306b __exit__ \u3092\u547c\u3076\u3068\u3044\u3046\u4ed5\u7d44\u307f\u306a\u306e\u3067\u3059\u304c\u3001\u30ab\u30fc\u30bd\u30eb\u306b\u306f\u3053\u308c\u3089\u306e\u30de\u30b8\u30c3\u30af\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\nTraceback (most recent call last):\r\n  File \".\/test_cursor_with.py\", line 29, in <module>\r\n    with conn.cursor() as cur:\r\nAttributeError: __enter__\r\n<\/pre>\n<\/blockquote>\n<p>\u305f\u3060\u3057\u3001 with \u3092\u629c\u3051\u308b\u3068\u304d\u306b close() \u3092\u547c\u3093\u3067\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308b closing \u3068\u3044\u3046\u30e9\u30c3\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u304c Python \u6a19\u6e96\u306e contextlib \u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u4e2d\u306b\u3042\u308b\u306e\u3067\u3001\u81ea\u524d\u3067\u30e9\u30c3\u30d1\u30fc\u3092\u66f8\u3044\u305f\u308a\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<blockquote>\n<pre>\r\nfrom contextlib import closing\r\nwith closing(conn.cursor()) as cursor:\r\n    cursor.execute(\"SELECT * FROM table\")\r\n    rows = cursor.fetchall()\r\n    ...\r\n<\/pre>\n<\/blockquote>\n<p>\u3055\u3066\u3001\u30ab\u30fc\u30bd\u30eb\u3092 with \u3067\u4f7f\u3048\u308b\u306a\u3089\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3067\u3082\u4f7f\u3063\u3066\u307f\u3088\u3046\u304b\u3068\u601d\u3046\u308f\u3051\u3067\u3059\u304c\u3001\u4f55\u3082\u8003\u3048\u305a\u306b with \u306b\u6e21\u3059\u3068\u304a\u304b\u3057\u306a\u6319\u52d5\u306b\u306a\u308a\u307e\u3059\u3002\uff08commit \u3084 rollback \u306b\u3064\u3044\u3066\u306f\u3068\u308a\u3042\u3048\u305a\u8003\u3048\u306a\u3044\u3053\u3068\u306b\u3057\u307e\u3059\uff09<\/p>\n<blockquote>\n<pre>\r\nwith pymysql.connect(**mysql_kwargs) as conn:\r\n    print(conn.__class__.__module__ + \".\" + conn.__class__.__name__)\r\n<\/pre>\n<\/blockquote>\n<blockquote>\n<pre>\r\npymysql.cursors.Cursor\r\n<\/pre>\n<\/blockquote>\n<p>\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u304c\u6765\u308b\u3053\u3068\u3092\u671f\u5f85\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u4f55\u6545\u304b\u30ab\u30fc\u30bd\u30eb\u306b\u306a\u3063\u3066\u6e21\u3063\u3066\u304d\u307e\u3059\u3002\u306a\u3093\u3067\u3084\u306d\u3093\u3002<\/p>\n<p>\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u578b\u3067\u3042\u308b pymysql.connections.Connection \u306e\u5b9a\u7fa9\u3092\u898b\u3066\u3082\u305d\u3082\u305d\u3082 __enter__ \u3068\u304b\u306f\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u304c\u671f\u5f85\u3055\u308c\u308b\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001\u306a\u3093\u3067\u3053\u3093\u306a\u6319\u52d5\u306b\u306a\u308b\u306e\u304b\u5206\u304b\u308a\u307e\u305b\u3093\u3002\u3046\u30fc\u3093\u3002<\/p>\n<p>\u4f7f\u7528\u74b0\u5883\u306f\u4ee5\u4e0b\u306e\u901a\u308a<\/p>\n<ul>\n<li>Ubuntu 18.04 LTS<\/li>\n<li>Python 3.6.8<\/li>\n<li>PyMySQL 0.8.0<\/li>\n<li>MySQL Connector\/Python 2.1.6<\/li>\n<li>mysqlclient 1.3.10<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u307e\u305f Python+MySQL \u30cd\u30bf\u3002 \u30ab\u30fc\u30bd\u30eb\u3092\u4f7f\u3063\u305f\u3089\u9589\u3058\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u3002\u51e6\u7406\u5f8c\u306b\u305d\u306e\u6d41\u308c\u3067\u305d\u306e\u307e\u307e cursor.close() \u3092\u547c\u3076\u3088\u3046\u306b\u66f8\u304f\u3068\u4f8b\u5916\u304c\u98db\u3093\u3060\u308a\u9577\u3044\u51e6\u7406\u306e\u9014\u4e2d\u3067\u3046\u3063\u304b\u308a return \u3057\u305f\u308a\u3057\u305f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-7317","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/7317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7317"}],"version-history":[{"count":5,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/7317\/revisions"}],"predecessor-version":[{"id":7322,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/7317\/revisions\/7322"}],"wp:attachment":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7317"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}