{"id":6784,"date":"2024-01-22T10:42:56","date_gmt":"2024-01-22T02:42:56","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=6784"},"modified":"2024-03-08T17:15:48","modified_gmt":"2024-03-08T09:15:48","slug":"sqlite-3-1-%e4%bd%bf%e7%94%a8-python3-%e5%ad%98%e5%8f%96%e8%b3%87%e6%96%99","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=6784","title":{"rendered":"Python3 - 1. SQLite 3 \u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/zh.wikipedia.org\/zh-tw\/SQLite\" target=\"_blank\" rel=\"noopener\">SQLite - \u7dad\u57fa\u767e\u79d1\uff0c\u81ea\u7531\u7684\u767e\u79d1\u5168\u66f8 (wikipedia.org)<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/3300464\/how-can-i-get-dict-from-sqlite-query\" target=\"_blank\" rel=\"noopener\">How can I get dict from sqlite query?<\/a><\/li>\n<li><a href=\"http:\/\/tw.gitbook.net\/sqlite\/sqlite_python.html\" target=\"_blank\" rel=\"noopener\">SQLite Python<\/a><\/li>\n<li><a href=\"https:\/\/likegeeks.com\/python-sqlite3-tutorial\/\" target=\"_blank\" rel=\"noopener\">Python SQLite3 Tutorial (Database Programming)<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u8a8d\u8b58 SQLite 3<\/h2>\n<p>SQLite 3 \u4f7f\u7528\u5728\u7a0b\u5f0f\u5167\uff0c\u4e5f\u5c31\u662f SQLite \u4e26\u4e0d\u662f\u50cf MySQL \u6216 Redis \u4e00\u6a23\u662f Server Client \u7684\u67b6\u69cb\uff0c\u9700\u8981\u5148\u5b89\u88dd Server \u5728\u4e3b\u6a5f\u4e0a\uff0c\u7136\u5f8c\u4f7f\u7528 Client \u53bb\u4f7f\u7528\u3002\u662f\u76f4\u63a5\u4f7f\u7528\u7a0b\u5f0f\u53bb\u5b58\u53d6\u8cc7\u6599\u5eab\uff0c\u5be6\u9ad4\u4e0a\u5c31\u662f\u4e00\u500b\u6a94\u6848\uff0c\u4e00\u500b\u6a94\u6848\u5c31\u4ee3\u8868\u6574\u500b\u8cc7\u6599\u5eab\u3002<\/p>\n<p>\u56e0\u70ba\u53ea\u662f\u4e00\u500b\u6a94\u6848\uff0c\u6240\u4ee5 SQLite 3 \u5efa\u8b70\u4e0d\u63a8\u85a6\u4f7f\u7528\u5728\u591a\u4eba\u540c\u6642\u74b0\u5883\u4e0a\uff0c\u56e0\u70ba SQLite 3 \u540c\u6642\u9593\u53ea\u80fd\u4e00\u500b\u5beb\u5165\u52d5\u4f5c\uff0c<\/p>\n<p>--<\/p>\n<h2>\u5b89\u88dd<\/h2>\n<p>SQLite 3 \u4e0d\u9700\u8981\u5b89\u88dd\uff0c\u53ea\u8981\u7a0b\u5f0f\u6709\u652f\u63f4\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002Hoyo \u5728\u67b6\u69cb\u8a2d\u8a08\u6642\u9084\u662f\u9700\u8981 GUI \u8f14\u52a9\uff0c\u6240\u4ee5\u6703\u5b89\u88dd <a href=\"https:\/\/sqlitebrowser.org\/\" target=\"_blank\" rel=\"noopener\">DB Browser for SQLite<\/a> \u4f86\u4f7f\u7528\u3002<\/p>\n<p>--<\/p>\n<h2>\u9023\u63a5\u8cc7\u6599\u5eab<\/h2>\n<p>Python 3 \u5167\u5efa SQLite 3\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<\/p>\n<pre class=\"lang:python decode:true\">import sqlite3\r\nconn = sqlite3.connect('Game.sqlite3', isolation_level=None)<\/pre>\n<p>\u5728\u6b63\u5f0f\u8cc7\u6599\u5b58\u53d6\u6642\uff0c\u5efa\u8b70\u5c07\u8cc7\u6599\u5eab\u9023\u63a5\u5305\u88dd\u6210\u4e00\u500b function<\/p>\n<pre class=\"lang:python decode:true\">def db_connect(self):\r\n    try:\r\n        conn = sqlite3.connect('Game.db', isolation_level=None)\r\n        conn.row_factory = sqlite3.Row  # \u52a0\u4e0a\u6b04\u4f4d\u540d\u7a31\r\n        return conn.cursor()\r\n    except Exception as e:\r\n        logging.error(e)<\/pre>\n<ul>\n<li>isolation_level=None<br \/>\n\u8a2d\u5b9a autocommit \u4e5f\u5c31\u662f\u95dc\u9589\u4e86 Transaction \u4ea4\u6613\u529f\u80fd\uff0c\u6839\u64da\u7a0b\u5f0f\u9700\u6c42\u8a2d\u5b9a\uff0c\u9810\u8a2d\u662f\u555f\u7528 Transaction<\/li>\n<li>row_factory = sqlite3.Row<br \/>\n\u8868\u793a\u56de\u50b3\u6b04\u4f4d\u540d\u7a31\uff0c\u56e0\u70ba Hoyo \u89ba\u5f97\u4f7f\u7528\u9806\u4f4d 0 1 2 3 \u5beb\u7a0b\u5f0f\u5f88\u53cd\u4eba\u985e<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u641c\u5c0b\u4e00\u7b46\u8cc7\u6599<\/h2>\n<pre class=\"lang:python decode:true\">sql = \" SELECT * FROM Player WHERE (RoomId, UserId)=(:RoomId, :UserId) \"\r\ncursor_select.execute(sql, {\r\n    'Card1': deck['Number'],\r\n    'RoomId': j['room_id'],\r\n    'UserId': j['user_id'],\r\n})\r\ncheck = cursor_select.fetchone()\r\nplayer_name = check['Name']<\/pre>\n<ul>\n<li>fetchone() \u53ea\u53d6\u4e00\u7b46\u8cc7\u6599<\/li>\n<li>\u56e0\u70ba\u8a2d\u5b9a\u4e86 sqlite3.Row \u6240\u4ee5\u53d6\u8cc7\u6599\u6642\u5c31\u662f\u4f7f\u7528\u6b04\u4f4d\u540d\u7a31<\/li>\n<li>:RoomId<br \/>\n\u5728 Prepared Statement \u4e5f\u662f\u4f7f\u7528\u540d\u7a31\uff0c? \u4e5f\u662f\u53cd\u4eba\u985e<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u641c\u5c0b\u591a\u7b46\u8cc7\u6599<\/h2>\n<pre class=\"lang:python decode:true \">cursor_select = self.db_connect()\r\n\r\nsql = \" SELECT * FROM Room WHERE Status=0 \"\r\ncursor_select.execute(sql)\r\nroom_rows = cursor_select.fetchall()\r\n\r\nfor row in room_rows:\r\n    print(row)<\/pre>\n<ul>\n<li>\u4f7f\u7528 fetchall() \u5c31\u662f\u53d6\u5f97 SELECT \u6240\u6709\u8cc7\u6599<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u63d2\u5165<\/h2>\n<pre class=\"lang:python decode:true\">from contextlib import closing\r\n\r\nwith closing(self.db_connect()) as cursor:\r\n    sql = \" INSERT INTO Deck ( RoomId, Number ) VALUES ( :RoomId, :Number ) \"\r\n    cursor.execute(sql, {\r\n        'RoomId': j['room_id'],\r\n        'Number': int(c),\r\n    })<\/pre>\n<ul>\n<li>SQLite 3 \u5beb\u5165\u90fd\u9700\u8981\u95dc\u9589\u8cc7\u6599\u5eab\uff0c\u8868\u793a\u78ba\u5b9a\u5beb\u5165\uff0c\u5728\u6b64\u4f7f\u7528 closing\uff0c\u4e00\u822c\u4f7f\u7528 close()<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u4fee\u6539<\/h2>\n<pre class=\"lang:python decode:true\">from contextlib import closing\r\n\r\nwith closing(self.db_connect()) as cursor:\r\n    sql = \" UPDATE Player SET Card2=:Card2 WHERE (RoomId, UserId)=(:RoomId, :UserId) \"\r\n    cursor.execute(sql, {\r\n        'Card2': deck['Number'],\r\n        'RoomId': j['room_id'],\r\n        'UserId': j['user_id'],\r\n    })<\/pre>\n<p>--<\/p>\n<h2>\u522a\u9664<\/h2>\n<pre class=\"lang:python decode:true\">from contextlib import closing\r\n\r\nwith closing(self.db_connect()) as cursor:\r\n    sql = \" DELETE FROM Room WHERE RoomId=:RoomId \"\r\n    cursor.execute(sql, {\r\n        'RoomId': j['room_id'],\r\n    })<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6784\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,133&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 SQLite ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6784\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,133&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[334],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6784"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6784"}],"version-history":[{"count":15,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6784\/revisions"}],"predecessor-version":[{"id":13134,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6784\/revisions\/13134"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}