{"id":7548,"date":"2024-10-08T15:34:04","date_gmt":"2024-10-08T07:34:04","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7548"},"modified":"2025-09-18T10:17:12","modified_gmt":"2025-09-18T02:17:12","slug":"sqlite-database-disk-image-is-malformed","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7548","title":{"rendered":"SQLite - database disk image is malformed"},"content":{"rendered":"<p>--<\/p>\n<h2>\u70ba\u4ec0\u9ebc\uff1f<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.sqlite.org\/howtocorrupt.html\">How To Corrupt An SQLite Database File<\/a><\/li>\n<\/ul>\n<p>\u6703\u9020\u6210\u8cc7\u6599\u5eab\u640d\u58de\u7684\u539f\u56e0\u5927\u90e8\u5206\u7684\u662f\u540c\u6642\u5beb\u5165\u3002\u6240\u4ee5\u958b\u767c\u81ea\u5df1\u6e2c\u8a66\u6642\u4e00\u5207\u6b63\u5e38\uff0c\u4e0a\u7dda\u5c31\u6703\u51fa\u554f\u984c\u3002<\/p>\n<p>--<\/p>\n<h2>\u4fee\u5fa9<\/h2>\n<p>\u5c0e\u51fa\u8cc7\u6599<\/p>\n<pre class=\"lang:default decode:true\">sqlite3.exe Game.db .dump &gt; dump.sql<\/pre>\n<p>\u5efa\u7acb\u65b0\u8cc7\u6599\u5eab\u53ca\u532f\u5165\u8cc7\u6599<\/p>\n<pre class=\"lang:default decode:true\">sqlite3.exe Game.db &lt; dump.sql<\/pre>\n<p>\u4f7f\u7528\u4e92\u52d5\u6a21\u5f0f<\/p>\n<pre class=\"lang:default decode:true \">sqlite3.exe Game.db\r\nsqlite&gt;.read dump.sql\r\nsqlite&gt;.quit<\/pre>\n<p>--<\/p>\n<h2>Python \u591a\u7dda\u7a0b\u8cc7\u6599\u5eab\u5beb\u5165\u4f47\u5217\u55ae\u4e00\u5beb\u5165<\/h2>\n<p>app.py<\/p>\n<pre class=\"lang:python decode:true \"># App.py \u4e3b\u8981\u90e8\u5206\r\nimport queue\r\n...\r\n\r\nclass App:\r\n    def __init__(self):\r\n        self.environment = 'company'\r\n        self.rs = None\r\n        self.cursor = None\r\n        self.client = None\r\n        self.room_play = {}\r\n\r\n        # === DB\u76f8\u95dc ===\r\n        self.db_queue = queue.Queue()\r\n        self.db_writer_running = True\r\n        self.db_lock = threading.Lock()\r\n        self.conn = self._init_db_conn()\r\n\r\n        threading.Thread(target=self._db_writer_thread, daemon=True).start()\r\n\r\n    def _init_db_conn(self):\r\n        if self.environment == 'company':\r\n            conn = sqlite3.connect('Game.db', isolation_level=None, check_same_thread=False)\r\n        else:\r\n            conn = sqlite3.connect('\/dev\/shm\/EquationHiLow.sqlite3', isolation_level=None, check_same_thread=False)\r\n        conn.row_factory = sqlite3.Row\r\n        return conn\r\n\r\n    def db_connect(self):\r\n        # \u53ea\u7528\u65bc SELECT\r\n        return self.conn.cursor()\r\n\r\n    def execute_db(self, sql, params=None):\r\n        self.db_queue.put((sql, params or {}))\r\n\r\n    def _db_writer_thread(self):\r\n        while self.db_writer_running:\r\n            try:\r\n                sql, params = self.db_queue.get()\r\n                with self.db_lock:\r\n                    self.conn.execute(sql, params)\r\n                    self.conn.commit()\r\n            except Exception as e:\r\n                logging.error(f\"[DB Write Error] {e}\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>db.py<\/p>\n<pre class=\"lang:python decode:true\">def run(client, self, j):\r\n    cursor_select = self.db_connect()\r\n\r\n    room_number = 0\r\n    i = 0\r\n    while i &lt; 10:\r\n        room_number = random.randint(100000, 999999)\r\n\r\n        sql = \"SELECT * FROM Room WHERE RoomId=:RoomId\"\r\n        cursor_select.execute(sql, {'RoomId': room_number})\r\n        room = cursor_select.fetchone()\r\n\r\n        if not room:\r\n            break\r\n        i += 1\r\n\r\n    try:\r\n        sql = \"SELECT * FROM Room WHERE OwnerId=:OwnerId\"\r\n        cursor_select.execute(sql, {'OwnerId': j['user_id']})\r\n        room = cursor_select.fetchone()\r\n\r\n        if room:\r\n            # \u6e05\u7406\u4f7f\u7528\u8005\u820a\u8cc7\u6599\uff08\u900f\u904e queue \u5beb\u5165\uff09\r\n            self.execute_db(\"DELETE FROM Room WHERE OwnerId=:OwnerId\", {'OwnerId': j['user_id']})\r\n            self.execute_db(\"DELETE FROM RoomStatus WHERE RoomId=:RoomId\", {'RoomId': room['RoomId']})\r\n            self.execute_db(\"DELETE FROM Player WHERE RoomId=:RoomId\", {'RoomId': room['RoomId']})\r\n\r\n        self.execute_db(\r\n            \"INSERT INTO Room (RoomId, Name, OwnerId, Status) VALUES (:RoomId, :Name, :OwnerId, 0)\",\r\n            {\r\n                'RoomId': room_number,\r\n                'Name': j['name'],\r\n                'OwnerId': j['user_id'],\r\n            }\r\n        )\r\n\r\n        self.execute_db(\r\n            \"INSERT INTO RoomStatus (RoomId, `Order`, People) VALUES (:RoomId, 1, :People)\",\r\n            {\r\n                'RoomId': room_number,\r\n                'People': int(j['people']),\r\n            }\r\n        )\r\n\r\n    except Exception as e:\r\n        logging.error(e)\r\n\r\n    send_command = {\r\n        \"command\": 'create_room_ok',\r\n    }\r\n    send_command = str(json.dumps(send_command))\r\n    client.publish('EquationHiLow\/' + j['user_id'], send_command.encode())\r\n\r\n    # \u547c\u53eb get_lobby \u66f4\u65b0\r\n    get_lobby_run(client, self, j)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7548\" 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;704&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u70ba\u4ec0\u9ebc\uff1f How To ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7548\" 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;704&nbsp;total views, &nbsp;2&nbsp;views today<\/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":[352],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7548"}],"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=7548"}],"version-history":[{"count":14,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7548\/revisions"}],"predecessor-version":[{"id":14385,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7548\/revisions\/14385"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}