{"id":7308,"date":"2019-08-03T19:39:17","date_gmt":"2019-08-03T10:39:17","guid":{"rendered":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=7308"},"modified":"2019-08-03T20:24:33","modified_gmt":"2019-08-03T11:24:33","slug":"pythonmysql-%e3%81%a7-dict-%e3%81%a7%e7%b5%90%e6%9e%9c%e3%81%8c%e8%bf%94%e3%81%a3%e3%81%a6%e3%81%8f%e3%82%8b%e3%82%ab%e3%83%bc%e3%82%bd%e3%83%ab%e3%82%92%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=7308","title":{"rendered":"Python+MySQL \u3067 dict \u3067\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304f\u308b\u30ab\u30fc\u30bd\u30eb\u3092\u4f7f\u3046"},"content":{"rendered":"<p>\u3061\u3087\u3063\u3068 Python + MySQL \u3092\u3044\u3058\u3063\u3066\u308b\u3093\u3067\u3059\u304c\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30af\u30a8\u30ea\u306e\u7d50\u679c\u306f\u30bf\u30d7\u30ebor\u30ea\u30b9\u30c8\u3067\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u96d1\u306b <code>SELECT * FROM table<\/code> \u3068\u304b\u3084\u308b\u3068\u3069\u3093\u306a\u9806\u756a\u3067\u5217\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u304b SQL \u6587\u3092\u898b\u305f\u3060\u3051\u3067\u306f\u4e0d\u660e\u3067\u6271\u3044\u3065\u3089\u3044\u305f\u3081\u3001 dict \uff08\u9023\u60f3\u914d\u5217\uff09\u3067\u53d6\u308c\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4f55\u3068\u304b\u306a\u3089\u306a\u3044\u306e\u304b\u306a\u3041\u3068\u601d\u3044\u3064\u3064 <a href=\"https:\/\/pymysql.readthedocs.io\/en\/latest\/modules\/index.html\">PyMySQL<\/a> \u3084 <a href=\"https:\/\/dev.mysql.com\/doc\/connector-python\/en\/connector-python-reference.html\">MySQL Connector\/Python<\/a> \u3084 <a href=\"https:\/\/mysqlclient.readthedocs.io\/index.html\">mysqlclient<\/a> \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3080\u3068\u3001 <code>connection.cursor()<\/code> \u306e\u5f15\u6570\u306b\u306a\u3093\u304b\u6e21\u3059\u3068\u7d50\u679c\u3092 dict \u3067\u8fd4\u3057\u3066\u304f\u308b\u30ab\u30fc\u30bd\u30eb\u304c\u5f97\u3089\u308c\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u3053\u308c\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\nmysql> SELECT * FROM table1;\r\n+------+------+\r\n| hoge | fuga |\r\n+------+------+\r\n|    1 |    2 |\r\n|    3 |    4 |\r\n+------+------+\r\n2 rows in set (0.00 sec)\r\n\r\nmysql> SELECT * FROM table2;\r\n+------+------+\r\n| fuga | piyo |\r\n+------+------+\r\n|    4 |    5 |\r\n+------+------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n<\/blockquote>\n<p>\u305d\u3057\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\n#!\/usr\/bin\/python3\r\n\r\nmysql_kwargs = {\r\n\t\"host\":     \"localhost\",\r\n\t\"port\":     3306,\r\n\t\"user\":     \"test\",\r\n\t\"password\": \"hogehoge\",\r\n\t\"database\": \"testdb\",\r\n}\r\n\r\n\r\nimport pymysql\r\nprint(\"PyMySQL\")\r\nprint()\r\n\r\nconn = pymysql.connect(**mysql_kwargs)\r\n\r\ndef get_cursor():\r\n\treturn conn.cursor()\r\n\r\ndef get_dict_cursor():\r\n\treturn conn.cursor(pymysql.cursors.DictCursor)\r\n\r\ncur = get_cursor()\r\ncur.execute(\"SELECT * FROM table1\")\r\nprint(cur.description)\r\nprint(cur.fetchall())\r\ncur.close()\r\n\r\ncur = get_dict_cursor()\r\ncur.execute(\"SELECT * FROM table1\")\r\nprint(cur.fetchall())\r\ncur.close()\r\n\r\nprint()\r\n\r\ncur = get_cursor()\r\ncur.execute(\"SELECT * FROM table1, table2 WHERE table1.fuga = table2.fuga\")\r\nprint(cur.description)\r\nprint(cur.fetchall())\r\ncur.close()\r\n\r\ncur = get_dict_cursor()\r\ncur.execute(\"SELECT * FROM table1, table2 WHERE table1.fuga = table2.fuga\")\r\nprint(cur.fetchall())\r\ncur.close()\r\n\r\nprint()\r\n\r\ncur = get_cursor()\r\ncur.execute(\"SELECT * FROM table1 INNER JOIN table2 USING(fuga)\")\r\nprint(cur.description)\r\nprint(cur.fetchall())\r\ncur.close()\r\n\r\ncur = get_dict_cursor()\r\ncur.execute(\"SELECT * FROM table1 INNER JOIN table2 USING(fuga)\")\r\nprint(cur.fetchall())\r\ncur.close()\r\n<\/pre>\n<\/blockquote>\n<blockquote>\n<pre>\r\n[umezawa@devubuntu:pts\/1 ~]$ .\/cursortest.py\r\nPyMySQL\r\n\r\n(('hoge', 3, None, 11, 11, 0, False), ('fuga', 3, None, 11, 11, 0, False))\r\n((1, 2), (3, 4))\r\n[{'hoge': 1, 'fuga': 2}, {'hoge': 3, 'fuga': 4}]\r\n\r\n(('hoge', 3, None, 11, 11, 0, False), ('fuga', 3, None, 11, 11, 0, False), ('fuga', 3, None, 11, 11, 0, False), ('piyo', 3, None, 11, 11, 0, False))\r\n((3, 4, 4, 5),)\r\n[{'hoge': 3, 'fuga': 4, 'table2.fuga': 4, 'piyo': 5}]\r\n\r\n(('fuga', 3, None, 11, 11, 0, False), ('hoge', 3, None, 11, 11, 0, False), ('piyo', 3, None, 11, 11, 0, False))\r\n((4, 3, 5),)\r\n[{'fuga': 4, 'hoge': 3, 'piyo': 5}]\r\n<\/pre>\n<\/blockquote>\n<p>\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u5148\u982d\u306e\u90e8\u5206\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u3048\u3066 MySQL Connector\/Python \u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\nimport mysql.connector\r\nprint(\"MySQL Connector\/Python\")\r\nprint()\r\n\r\nconn = mysql.connector.connect(**mysql_kwargs)\r\n\r\ndef get_cursor():\r\n\treturn conn.cursor()\r\n\r\ndef get_dict_cursor():\r\n\treturn conn.cursor(dictionary=True)\r\n<\/pre>\n<\/blockquote>\n<blockquote>\n<pre>\r\n[umezawa@devubuntu:pts\/1 ~]$ .\/cursortest.py\r\nMySQL Connector\/Python\r\n\r\n[('hoge', 3, None, None, None, None, 0, 4097), ('fuga', 3, None, None, None, None, 0, 4097)]\r\n[(1, 2), (3, 4)]\r\n[{'hoge': 1, 'fuga': 2}, {'hoge': 3, 'fuga': 4}]\r\n\r\n[('hoge', 3, None, None, None, None, 0, 4097), ('fuga', 3, None, None, None, None, 0, 4097), ('fuga', 3, None, None, None, None, 0, 4097), ('piyo', 3, None, None, None, None, 0, 4097)]\r\n[(3, 4, 4, 5)]\r\n[{'hoge': 3, 'fuga': 4, 'piyo': 5}]\r\n\r\n[('fuga', 3, None, None, None, None, 0, 4097), ('hoge', 3, None, None, None, None, 0, 4097), ('piyo', 3, None, None, None, None, 0, 4097)]\r\n[(4, 3, 5)]\r\n[{'fuga': 4, 'hoge': 3, 'piyo': 5}]\r\n<\/pre>\n<\/blockquote>\n<p>\u4eca\u5ea6\u306f\u3053\u3046\u66f8\u304d\u63db\u3048\u3066 mysqlclient \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\nimport MySQLdb\r\nprint(\"mysqlclient\")\r\nprint()\r\n\r\nconn = MySQLdb.connect(**mysql_kwargs)\r\n\r\ndef get_cursor():\r\n\treturn conn.cursor()\r\n\r\ndef get_dict_cursor():\r\n\treturn conn.cursor(MySQLdb.cursors.DictCursor)\r\n<\/pre>\n<\/blockquote>\n<blockquote>\n<pre>\r\n[umezawa@devubuntu:pts\/1 ~]$ .\/cursortest.py\r\nmysqlclient\r\n\r\n(('hoge', 3, 1, 11, 11, 0, 0), ('fuga', 3, 1, 11, 11, 0, 0))\r\n((1, 2), (3, 4))\r\n({'hoge': 1, 'fuga': 2}, {'hoge': 3, 'fuga': 4})\r\n\r\n(('hoge', 3, 1, 11, 11, 0, 0), ('fuga', 3, 1, 11, 11, 0, 0), ('fuga', 3, 1, 11, 11, 0, 0), ('piyo', 3, 1, 11, 11, 0, 0))\r\n((3, 4, 4, 5),)\r\n({'hoge': 3, 'fuga': 4, 'table2.fuga': 4, 'piyo': 5},)\r\n\r\n(('fuga', 3, 1, 11, 11, 0, 0), ('hoge', 3, 1, 11, 11, 0, 0), ('piyo', 3, 1, 11, 11, 0, 0))\r\n((4, 3, 5),)\r\n({'fuga': 4, 'hoge': 3, 'piyo': 5},)\r\n<\/pre>\n<\/blockquote>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067\u4f55\u3068\u306a\u304f\u671f\u5f85\u3057\u305f\u3088\u3046\u306a\u7d50\u679c\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>MySQL Connector\/Python \u306f\u3001\u540c\u3058\u540d\u524d\u306e\u5217\u304c\u8907\u6570\u51fa\u73fe\u3059\u308b\u3068\u304d\u306e\u6319\u52d5\u304c\u4ed6\u306e2\u3064\u3068\u7570\u306a\u308b\uff08\u4ed6\u306e2\u3064\u306f2\u3064\u76ee\u306e\u51fa\u73fe\u306b\u306f\u30c6\u30fc\u30d6\u30eb\u540d\u304c\u4ed8\u304f\u306e\u3067\u898b\u5206\u3051\u304c\u3064\u304f\u304c\u3001MySQL Connector\/Python \u306f\u30c6\u30fc\u30d6\u30eb\u540d\u306f\u4ed8\u304b\u305a\u306b\u4e0a\u66f8\u304d\u3055\u308c\u308b\u3063\u307d\u3044\uff1f\uff09\u306e\u3067\u3001\u305d\u306e\u70b9\u3060\u3051\u6ce8\u610f\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b dict \u306a\u30ab\u30fc\u30bd\u30eb\u304c\u4f7f\u3048\u306a\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a idiom \u3067\u540c\u69d8\u306e\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u6700\u521d\u306f\u5168\u90e8\u3053\u3046\u66f8\u3044\u3066\u3066\u30c0\u30b5\u3044\u306a\u3041\u3068\u601d\u3063\u3066\u305f\uff09<\/p>\n<blockquote>\n<pre>\r\ndict_results= [dict((cursor.description[i][0], value) for i, value in enumerate(row)) for row in cursor.fetchall()]\r\n<\/pre>\n<\/blockquote>\n<p>\u3061\u306a\u307f\u306b\u4f7f\u3063\u305f\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>Ubuntu 18.04 LTS<\/li>\n<li>Python 3.6.8<\/li>\n<li>PyMySQL 0.9.3<\/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>\u3061\u3087\u3063\u3068 Python + MySQL \u3092\u3044\u3058\u3063\u3066\u308b\u3093\u3067\u3059\u304c\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30af\u30a8\u30ea\u306e\u7d50\u679c\u306f\u30bf\u30d7\u30ebor\u30ea\u30b9\u30c8\u3067\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u96d1\u306b SELECT * FROM table \u3068\u304b\u3084\u308b\u3068\u3069\u3093\u306a\u9806\u756a\u3067\u5217\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u304b SQL  [&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-7308","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\/7308","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=7308"}],"version-history":[{"count":7,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/7308\/revisions"}],"predecessor-version":[{"id":7315,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/7308\/revisions\/7315"}],"wp:attachment":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7308"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}