{"id":5122,"date":"2020-10-09T20:38:58","date_gmt":"2020-10-09T11:38:58","guid":{"rendered":"http:\/\/kats-eye.net\/info\/?p=5122"},"modified":"2020-10-09T21:10:12","modified_gmt":"2020-10-09T12:10:12","slug":"phytho-sqlite3","status":"publish","type":"post","link":"https:\/\/kats-eye.net\/info\/2020\/10\/09\/phytho-sqlite3\/","title":{"rendered":"PYTHON \u3067 SQLITE3 \u3092\u4f7f\u3046"},"content":{"rendered":"\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u6982\u8981<\/h2>\n\n\n\n<p>\u3000python \u304b\u3089WEB\u691c\u7d22\u3059\u308b\u6642\u306b\u6bd4\u8f03\u7684\u305f\u304f\u3055\u3093\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u6642\u7684\u306b\u84c4\u7a4d\u3059\u308b\u5834\u5408\u3092\u60f3\u5b9a\u3057\u3066\u3001 sqlite3 \u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3057\u305f\u3002 sqlite3 \u306f\u3001\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u4f7f\u7528\u3067\u304d\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u3067\u624b\u8efd\u306b\u4f7f\u7528\u3067\u304d\u308b\u5229\u70b9\u304c\u3042\u308a\u307e\u3059\u3002 <br>\u3000sqlite3 \u306f python \u306e\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001\u7279\u306b\u8ffd\u52a0\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u305b\u305a\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u4f7f\u7528\u65b9\u6cd5\u3092\u7c21\u5358\u306b\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002<br><\/p>\n\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">SQLite Browser<\/h2>\n\n\n\n<p>\u3000sqlite \u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001python  \u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u7de8\u96c6\u3057\u307e\u3059\u304c\u3001\u7d50\u679c\u3092\u78ba\u8a8d\u3057\u305f\u3044\u6642\u306a\u3069\u306b \u30d6\u30e9\u30a6\u30b6\u30fc\uff08\u30a8\u30c7\u30a3\u30bf\u30fc\uff09\u304c\u3042\u308b\u3068 \u4fbf\u5229\u306a\u306e\u3067\u3001\u201c DB Browser for SQLite \u201d \u3068\u3044\u3046\u30d6\u30e9\u30a6\u30b6\u30fc\u3092\u6700\u521d\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"558\" height=\"495\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/001.png\" alt=\"\" class=\"wp-image-5128\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/001.png 558w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/001-300x266.png 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><figcaption>          \u5f15\u7528 \uff1a DB Browser for SQLite\uff08  https:\/\/sqlitebrowser.org\/  \uff09<\/figcaption><\/figure>\n\n\n\n<p>\u3000\u4eca\u56de\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30da\u30fc\u30b8\u306e\u30ea\u30b9\u30c8\u9805\u76ee\u306e\u4e2d\u306e\u6b21\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30e9\u30fc\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u305f\u3002<br>\u3000\uff08PC\u74b0\u5883\uff1aWindows10 Pro 64bit\uff09<br>[ <a href=\"https:\/\/download.sqlitebrowser.org\/DB.Browser.for.SQLite-3.12.0-win64.msi\">DB Browser for SQLite &#8211; Standard installer for 64-bit Windows<\/a> ] <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"603\" height=\"360\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/002.png\" alt=\"\" class=\"wp-image-5130\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/002.png 603w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/002-300x179.png 300w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><figcaption>\u3000\u5f15\u7528\uff08\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30da\u30fc\u30b8\uff09 \uff1a https:\/\/sqlitebrowser.org\/dl\/<\/figcaption><\/figure>\n\n\n\n<p>\u3000\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u4e0b\u8a18\u306e\u624b\u9806\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"718\" height=\"892\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/030.png\" alt=\"\" class=\"wp-image-5147\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/030.png 718w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/030-241x300.png 241w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff08python\uff09<\/h2>\n\n\n\n<h2>\u2460 \u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u30fb\u30c7\u30fc\u30bf\u66f8\u304d\u8fbc\u307f<\/h2>\n\n\n\n<p>\u3000\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u30fb\u30c7\u30fc\u30bf\u66f8\u304d\u8fbc\u307f\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># sqlite_00_create_table.py\n# Windows10 Pro 64bit\n# Python 3.8.3\nimport sqlite3\n \ndbpath = 'db_sample.sqlite'                         # \uff83\uff9e\uff70\uff80\uff8d\uff9e\uff70\uff7d\uff8c\uff67\uff72\uff99\uff8a\uff9f\uff7d\nconnection = sqlite3.connect(dbpath)                # \uff83\uff9e\uff70\uff80\uff8d\uff9e\uff70\uff7d\u63a5\u7d9a\n# connection.isolation_level = None                 # \u81ea\u52d5\uff7a\uff90\uff6f\uff84\u6642\u3001\uff7a\uff92\uff9d\uff84\uff71\uff73\uff84\u89e3\u9664\ncursor = connection.cursor()                        # \uff76\uff70\uff7f\uff99\u751f\u6210\n \n# \u30a8\u30e9\u30fc\u51e6\u7406\uff08\u4f8b\u5916\u51e6\u7406\uff09\ntry:\n    # CREATE\uff08\u30c6\u30fc\u30d6\u30eb\u751f\u6210\uff09\n    cursor.execute(\"DROP TABLE IF EXISTS sample\")   # \u65e2\u306b\uff83\uff70\uff8c\uff9e\uff99\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u524a\u9664\n    cursor.execute(\n        \"CREATE TABLE IF NOT EXISTS sample (id INTEGER PRIMARY KEY, name TEXT)\")\n \n    # INSERT\uff08\u30c7\u30fc\u30bf\u633f\u5165\uff09\n    cursor.execute(\"INSERT INTO sample VALUES (1, '\u308a\u3093\u3054')\")\n    # \u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u306e\u4f7f\u7528\u4f8b\n    # \u7591\u554f\u7b26(qmark \u30b9\u30bf\u30a4\u30eb)\u3068\u540d\u524d(named \u30b9\u30bf\u30a4\u30eb)\u306e2\u65b9\u6cd5\n    # 1\u3064\u306e\u5834\u5408\u3001\u6700\u5f8c\u306b\u201c,\u201d\u304c\u306a\u3044\u3068\u30a8\u30e9\u30fc('\u307f\u304b\u3093') \u2192 ('\u307f\u304b\u3093',)\n    cursor.execute(\"INSERT INTO sample VALUES (2, ?)\", ('\u307f\u304b\u3093',))\n    cursor.execute(\"INSERT INTO sample VALUES (?, ?)\", (3, '\u3070\u306a\u306a'))\n    cursor.execute(\"INSERT INTO sample VALUES (:id, :name)\",\n                   {'id': 4, 'name': '\u304b\u304d'})\n    \n    # \u8907\u6570\u30ec\u30b3\u30fc\u30c9\u540c\u6642\u633f\u5165\n    fruit_name = [\n        (5, '\u306a\u3057'),\n        (6, '\u3076\u3069\u3046'),\n    ]\n    \n    cursor.executemany(\"INSERT INTO sample VALUES (?, ?)\", fruit_name)\n\nexcept sqlite3.Error as e:\n    print('sqlite3.Error occurred:', e.args[0])\n \nconnection.commit()                                 # \u51e6\u7406\u5b9f\u884c\nconnection.close()                                  # \u63a5\u7d9a\u3092\u9589\u3058\u308b<\/code><\/pre>\n\n\n\n<p>\u3000 \u201c DB Browser for SQLite \u201d \u3067\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304d\u3001\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3001\u30c7\u30fc\u30bf\u304c\u6b63\u3057\u304f\u66f8\u304d\u8fbc\u307e\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002 <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"721\" height=\"574\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/003.png\" alt=\"\" class=\"wp-image-5133\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/003.png 721w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/003-300x239.png 300w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<h2>\u2461 \u30c7\u30fc\u30bf\u691c\u7d22<\/h2>\n\n\n\n<p>\u3000\u30c7\u30fc\u30bf\u691c\u7d22\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002\u691c\u7d22\u7d50\u679c\u306e\u51fa\u529b\u65b9\u6cd5\u3092\u5e7e\u3064\u304b\u5909\u3048\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># sqlite_01_select.py\nimport sqlite3\n \ndb_path = \"db_sample.sqlite\"                                # DB\uff8c\uff67\uff72\uff99\uff8a\uff9f\uff7d\ntbl_nam = \"sample\"                                          # \uff83\uff70\uff8c\uff9e\uff99\u540d\n\nsql = 'SELECT * FROM ' + tbl_nam + ' ORDER BY id DESC'\n\nconnection = sqlite3.connect(db_path)                       # DB\u63a5\u7d9a\ncursor = connection.cursor()                                # \uff76\uff70\uff7f\uff99\u751f\u6210\n\nprint()\nprint('\u25a0 \u672a\u52a0\u5de5\u53d6\u5f97\u30c7\u30fc\u30bf\u5168\u51fa\u529b \u25a0')\ncursor.execute(sql)\nres = cursor.fetchall()\nprint(res)\n\n\n# \u2460\u914d\u5217\nprint()\nprint('\u25a0 \u2460\u914d\u5217 \u25a0')\nfor idx_row in range(len(res)):\n    fetch_data = \"\"\n    for idx_col in range(len(res[idx_row])):\n        try:\n            fetch_data += res[idx_row][idx_col]\n        except:\n            fetch_data += str(res[idx_row][idx_col])\n            \n        if(idx_col &lt; len(res[idx_row]) - 1 ):\n            fetch_data += ' , '\n            \n    print(fetch_data)\n\n\n# \u2461iterator\nprint()\nprint('\u25a0 \u2461iterator \u25a0')\nfor row in cursor.execute(sql):\n    print(row)\n \n\n# \u2462fetchall()\nprint()\nprint('\u25a0 \u2462fetchall \u25a0')\ncursor.execute(sql)\nfor row in cursor.fetchall():\n    print(row)\n\n# \u2463\u914d\u5217\nprint()\nprint('\u25a0 \u2463\u914d\u5217 \u25a0')\nfor idx_row in range(len(res)):\n    fetch_data = \"\"\n    for idx_col in range(len(res[idx_row])):\n        try:\n            fetch_data += res[idx_row][idx_col]\n        except:\n            fetch_data += str(res[idx_row][idx_col])\n            \n        if(idx_col &lt; len(res[idx_row]) - 1 ):\n            fetch_data += ' , '\n            \n    print(fetch_data)\n\n\nconnection.close()                                          # DB\u63a5\u7d9a\u89e3\u9664\nprint()<\/code><\/pre>\n\n\n\n<p><br>\u3000\u7d50\u679c\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"643\" height=\"580\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/004.png\" alt=\"\" class=\"wp-image-5135\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/004.png 643w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/004-300x271.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<h2>\u2462 \u30c7\u30fc\u30bf\u66f4\u65b0\u30fb\u524a\u9664<\/h2>\n\n\n\n<p>\u3000\u30c7\u30fc\u30bf\u66f4\u65b0\u30fb\u524a\u9664\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># sqlite_02_update.py\nimport sqlite3\nimport random\n \ndb_path = \"db_sample.sqlite\"                                # DB\uff8c\uff67\uff72\uff99\uff8a\uff9f\uff7d\ntbl_nam = \"sample\"                                          # \uff83\uff70\uff8c\uff9e\uff99\u540d\n\nsql = 'SELECT * FROM ' + tbl_nam + ' ORDER BY id'\n\nconnection = sqlite3.connect(db_path)                       # DB\u63a5\u7d9a\ncursor = connection.cursor()                                # \uff76\uff70\uff7f\uff99\u751f\u6210\ncursor.execute(sql)\nres = cursor.fetchall()\n\n# \u2460\u66f4\u65b0\u524d\u30c7\u30fc\u30bf\u8868\u793a\nprint()\nprint('\u25a0 \u2460\u66f4\u65b0\u524d\u30c7\u30fc\u30bf \u25a0')\ncursor.execute(sql)\nfor row in cursor.fetchall():\n    print(row)\n\n\n\n# \u66f4\u65b0\u51e6\u7406\ncursor.execute(\"UPDATE \" + tbl_nam + \" SET name=? WHERE id=?\" , ( \"\u30ad\u30e5\u30a6\u30a4\" , 1 ,))\ncursor.execute(\"UPDATE \" + tbl_nam + \" SET name=? WHERE id=?\" , ( \"\u30b6\u30af\u30ed\" , 3 ,))\nconnection.commit()\n\n# \u2461\u66f4\u65b0\u5f8c\u30c7\u30fc\u30bf\u8868\u793a\nprint()\nprint('\u25a0 \u2461\u66f4\u65b0\u5f8c\u30c7\u30fc\u30bf \u25a0')\ncursor.execute(sql)\nfor row in cursor.fetchall():\n    print(row)\n\n\n\n# \u62b9\u6d88\u51e6\u7406\ncursor.execute(\"DELETE FROM \" + tbl_nam + \" WHERE id>=?\" , ( 5 ,))\nconnection.commit()\n\n# \u2462\u62b9\u6d88\u5f8c\u30c7\u30fc\u30bf\u8868\u793a\nprint()\nprint('\u25a0 \u2462\u62b9\u6d88\u5f8c\u30c7\u30fc\u30bf \u25a0')\ncursor.execute(sql)\nfor row in cursor.fetchall():\n    print(row)\n\n\n\nconnection.close()                                          # DB\u63a5\u7d9a\u89e3\u9664\nprint()\n\n<\/code><\/pre>\n\n\n\n<p><br>\u3000\u2460\u66f4\u65b0\u524d\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066 \u2461\u66f4\u65b0\u5f8c\u30c7\u30fc\u30bf\u306f\u3001No1\uff0cNo3 \u306e\u679c\u7269\u540d\u304c\u5909\u66f4\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u66f4\u306b\u2462\u62b9\u6d88\u5f8c\u30c7\u30fc\u30bf\u306f\u3001No5\uff0cNo6 \u306e\u30c7\u30fc\u30bf\u304c\u7121\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"695\" height=\"411\" src=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/005.png\" alt=\"\" class=\"wp-image-5139\" srcset=\"https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/005.png 695w, https:\/\/kats-eye.net\/info\/wp-content\/uploads\/2020\/10\/005-300x177.png 300w\" sizes=\"(max-width: 695px) 100vw, 695px\" \/><figcaption><br><br><\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-text-color\" style=\"color:#0000ff\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3000\u6b21\u56de\u306f\u3001\u5b9f\u969b\u306b Selenuim \u3092\u4f7f\u3063\u3066\u3001\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092 sqlite3 \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306b\u9023\u7d9a\u7684\u306b\u84c4\u7a4d\u3059\u308b\u51e6\u7406\u3092\u30c6\u30b9\u30c8\u3059\u308b\u4e88\u5b9a\u3067\u3059\u3002<br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u3000python \u304b\u3089WEB\u691c\u7d22\u3059\u308b\u6642\u306b\u6bd4\u8f03\u7684\u305f\u304f\u3055\u3093\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u6642\u7684\u306b\u84c4\u7a4d\u3059\u308b\u5834\u5408\u3092\u60f3\u5b9a\u3057\u3066\u3001 sqlite3 \u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3057\u305f\u3002 sqlite3 \u306f\u3001\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u4f7f\u7528\u3067\u304d\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u3067\u624b\u8efd\u306b\u4f7f\u7528\u3067\u304d &hellip; <a href=\"https:\/\/kats-eye.net\/info\/2020\/10\/09\/phytho-sqlite3\/\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">PYTHON \u3067 SQLITE3 \u3092\u4f7f\u3046<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":5128,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75,101],"tags":[116,151],"_links":{"self":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/5122"}],"collection":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/comments?post=5122"}],"version-history":[{"count":17,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/5122\/revisions"}],"predecessor-version":[{"id":5364,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/posts\/5122\/revisions\/5364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/media\/5128"}],"wp:attachment":[{"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/media?parent=5122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/categories?post=5122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kats-eye.net\/info\/wp-json\/wp\/v2\/tags?post=5122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}