{"id":376,"date":"2018-12-11T11:35:19","date_gmt":"2018-12-11T03:35:19","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=376"},"modified":"2022-05-17T09:44:28","modified_gmt":"2022-05-17T01:44:28","slug":"sqllite","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=376","title":{"rendered":"\u901a\u8fc7Queue\u89e3\u51b3sqllite\u591a\u7ebf\u7a0b\u62a5\u9519\u7684\u95ee\u9898(\u5b9e\u73b0\u591a\u7ebf\u7a0b\u589e\u5220\u6539\u67e5\uff0c\u4ee5\u5b57\u5178\u5f62\u5f0f\u67e5\u8be2\u7ed3\u679c)"},"content":{"rendered":"<h1>\u9700\u6c42\uff1a<\/h1>\n<p>\u5c0f\u7a0b\u5e8f\u540e\u53f0\u7528\u7684sqllite\u6570\u636e\u5e93\uff0c\u521a\u5f00\u59cb\u7528\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u8003\u8651\u591a\u7ebf\u7a0b\uff0c\u800c\u4e14\u5f53\u65f6\u56e0\u4e3a\u6570\u636e\u91cf\u5c11\uff0c\u6ca1\u6709\u51fa\u73b0\u8fc7\u591a\u7ebf\u7a0b\u67e5\u8be2\u62a5\u9519\uff0c\u73b0\u5728\u6570\u636e\u91cf\u5927\u4e86\u3002\u591a\u7ebf\u7a0b\u67e5\u8be2\u7ecf\u5e38\u62a5\u9519<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">ProgrammingError: Recursive use of cursors not allowed.<\/code><\/pre>\n<p>\u5c31\u662f\u8fd9\u4e2a\u5934\u75bc\u7684\u9519\u3002\u5728\u7f51\u4e0a\u67e5\u4e86\u5927\u91cf\u7684\u8d44\u6599\uff0c\u8981\u4e48\u5c31\u662f\u52a0lock=threading.lock(),\u8981\u4e48\u5c31\u662f\u52a0sleep.\u7ec8\u7a76\u8fd8\u662f\u89e3\u51b3\u4e0d\u4e86\u95ee\u9898\u3002<br \/>\n\u521a\u597d\u6700\u8fd1\u5728\u7f51\u4e0a\u770b\u4e86\u4e00\u4e2a\u5c0f\u54e5\u54e5\u7528Queue\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6211\u6539\u8fdb\u4e86\u4e00\u4e0b\u3002\u76ee\u524d\u80fd\u591f\u4f7f\u7528\u8be5\u65b9\u6cd5\u8fdb\u884c\u589e\u5220\u6539\u67e5\u3002\u67e5\u8be2\u51fa\u6765\u7684\u7ed3\u679c\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<br \/>\n\u8bdd\u4e0d\u591a\u8bf4\uff0c\u4e0b\u9762\u4e0a\u4ee3\u7801<\/p>\n<h1>\u4ee3\u7801<\/h1>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding: UTF-8 -*-\nimport sqlite3\nimport time\nfrom Queue import Queue\nfrom threading import Thread\ndef sqllite_escape(key_word):\n    key_word = key_word.encode(\"utf-8\")\n    key_word = key_word.replace(\"'\", \"''\")\n    return key_word\nclass SelectConnect(object):\n    '''\n    \u53ea\u80fd\u7528\u6765\u67e5\u8be2\n    '''\n    def __init__(self):\n        # isolation_level=None\u4e3a\u667a\u80fd\u63d0\u4ea4\u6a21\u5f0f\uff0c\u4e0d\u9700\u8981commit\n        self.conn = sqlite3.connect('resource\/data.ta', check_same_thread=False, isolation_level=None)\n        self.conn.execute('PRAGMA journal_mode = WAL')\n        cursor = self.conn.cursor()\n        cursor.execute('PRAGMA synchronous=OFF')\n        self.conn.text_factory = str\n        # \u628a\u7ed3\u679c\u7528\u5143\u7956\u7684\u5f62\u5f0f\u53d6\u51fa\u6765\n        self.curosr = self.conn.cursor()\n        self.conn.row_factory = self.dict_factory\n        # \u628a\u7ed3\u679c\u7528\u5b57\u5178\u7684\u5f62\u5f0f\u53d6\u51fa\u6765\n        self.curosr_diction = self.conn.cursor()\n    def commit(self):\n        self.conn.commit()\n    def dict_factory(self, cursor, row):\n        d = {}\n        for idx, col in enumerate(cursor.description):\n            d[col[0]] = row[idx]\n        return d\n    def close_db(self):\n        # self.curosr.close()\n        self.conn.close()\nclass SqliteMultithread(Thread):\n    \"\"\"\n    Wrap sqlite connection in a way that allows concurrent requests from multiple threads.\n    This is done by internally queueing the requests and processing them sequentially\n    in a separate thread (in the same order they arrived).\n    \"\"\"\n    def __init__(self, filename, autocommit, journal_mode):\n        super(SqliteMultithread, self).__init__()\n        self.filename = filename\n        self.autocommit = autocommit\n        self.journal_mode = journal_mode\n        self.reqs = Queue()  # use request queue of unlimited size\n        self.setDaemon(True)  # python2.5-compatible\n        self.running = True\n        self.start()\n    def dict_factory(self, cursor, row):\n        # field = [i[0] for i in cursor.description]\n        # value = [dict(zip(field, i)) for i in records]\n        d = {}\n        for idx, col in enumerate(cursor.description):\n            d[col[0]] = row[idx]\n        return d\n    def run(self):\n        if self.autocommit:\n            conn = sqlite3.connect(self.filename, isolation_level=None, check_same_thread=False)\n        else:\n            conn = sqlite3.connect(self.filename, check_same_thread=False)\n        conn.execute('PRAGMA journal_mode = %s' % self.journal_mode)\n        conn.text_factory = str\n        cursor = conn.cursor()\n        cursor.execute('PRAGMA synchronous=OFF')\n        conn.row_factory = self.dict_factory\n        curosr_diction = conn.cursor()\n        curosr_diction.execute('PRAGMA synchronous=OFF')\n        # \u628a\u7ed3\u679c\u7528\u5b57\u5178\u7684\u5f62\u5f0f\u53d6\u51fa\u6765\n        while self.running:\n            req, arg, res = self.reqs.get()\n            if req == '--close--':\n                break\n            elif req == '--commit--':\n                conn.commit()\n            else:\n                # print(arg)\n                curosr_diction.execute(req, arg)\n                # if res:\n                #     for rec in cursor:\n                #         res.put(rec)\n                #     res.put('--no more--')\n                if res:\n                    res.put(curosr_diction.fetchall())\n                if self.autocommit:\n                    conn.commit()\n        conn.close()\n    def execute(self, req, arg=None, res=None):\n        \"\"\"\n        `execute` calls are non-blocking: just queue up the request and return immediately.\n        \"\"\"\n        self.reqs.put((req, arg or tuple(), res))\n    def executemany(self, req, items):\n        for item in items:\n            self.execute(req, item)\n    def select_all_dict(self, req, arg=None):\n        '''\n        \u76f4\u63a5\u8fd4\u56de\u4e00\u4e2alist\n        :param req:\n        :param arg:\n        :return:\n        '''\n        res = Queue()  # results of the select will appear as items in this queue\n        self.execute(req, arg, res)\n        rec = res.get()\n        return rec\n    def select_one_dict(self, req, arg=None):\n        '''\n        \u76f4\u63a5\u8fd4\u56delist\u91cc\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u4ee5\u5b57\u5178\u5c55\u793a\n        :param req:\n        :param arg:\n        :return:\n        '''\n        res = Queue()  # results of the select will appear as items in this queue\n        self.execute(req, arg, res)\n        rec = res.get()\n        if len(rec) != 0:\n            rec = rec[0]\n        else:\n            rec = None\n        return rec\n    def commit(self):\n        self.execute('--commit--')\n    def close(self):\n        self.execute('--close--')\nclass Cursor(object):\n    '''\n    \u4ee5\u5143\u7956\u7684\u5f62\u5f0f\u67e5\u8be2\u51fa\u6570\u636e\n    '''\n    def __init__(self):\n        old_con = SelectConnect()\n        self.conn = old_con.conn\n        self.curosr = old_con.curosr\n        self.curosr2 = SqliteMultithread('resource\/data.ta', autocommit=True, journal_mode=\"WAL\")\n    def execute(self, string, *args):\n        try:\n            if string.startswith('select'):\n                return self.curosr.execute(string, *args)\n            else:\n                return self.curosr2.execute(string, *args)\n        except Exception:\n            print(\"\u5931\u8d25\u4e00\u6b21\")\n            print(string)\n            time.sleep(0.1)\n            self.execute(string, *args)\n    def executescript(self, string):\n        try:\n            self.curosr.executescript(string)\n        except Exception:\n            print(\"\u5931\u8d25\u4e00\u6b21\")\n            print(string)\n            time.sleep(0.1)\n            self.executescript(string)\n    def fetchall(self):\n        return self.curosr.fetchall()\n    def fetchone(self):\n        return self.curosr.fetchone()\n    def rowcount(self):\n        return self.curosr.rowcount\n    def close(self):\n        self.curosr2.running = False\n        self.curosr.close()\n        self.conn.close()\nclass Curosrdiction(object):\n    '''\n    \u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u67e5\u8be2\u51fa\u6570\u636e\uff0c\u5efa\u8bae\u5168\u90e8\u7528\u8fd9\u79cd\u3002\n    '''\n    def __init__(self):\n        old_con = SelectConnect()\n        self.conn = old_con.conn\n        self.curosrdiction = old_con.curosr_diction\n        self.curosr2 = SqliteMultithread('resource\/data.ta', autocommit=True, journal_mode=\"WAL\")\n    def execute(self, string, *args):\n        try:\n            if string.startswith('select'):\n                return self.curosrdiction.execute(string, *args)\n            else:\n                return self.curosr2.execute(string, *args)\n        except Exception:\n            print(\"\u5931\u8d25\u4e00\u6b21\")\n            print(string)\n            time.sleep(0.1)\n            self.execute(string, *args)\n    def executescript(self, string):\n        result = True\n        try:\n            self.curosrdiction.executescript(string)\n        except Exception:\n            print(\"\u5931\u8d25\u4e00\u6b21\")\n            # print(string)\n            time.sleep(0.1)\n            # self.executescript(string)\n            result = False\n        return result\n    def fetchall(self):\n        return self.curosrdiction.fetchall()\n    def fetchone(self):\n        return self.curosrdiction.fetchone()\n    def rowcount(self):\n        return self.curosrdiction.rowcount\n    def select_all_dict(self, string, *args):\n        return self.curosr2.select_all_dict(string, *args)\n    def select_one_dict(self, string, *args):\n        return self.curosr2.select_one_dict(string, *args)\n    def close(self):\n        self.curosr2.running = False\n        self.curosrdiction.close()\n        self.conn.close()\n    def commit(self):\n        self.conn.commit()\n        self.curosr2.commit()\n# curosr = Cursor()\ncurosr_diction = Curosrdiction()\ndef commit():\n    curosr_diction.commit()\ndef close_db():\n    # curosr.close()\n    curosr_diction.close()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u5c0f\u7a0b\u5e8f\u540e\u53f0\u7528\u7684sqllite\u6570\u636e\u5e93\uff0c\u521a\u5f00\u59cb\u7528\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u8003\u8651\u591a\u7ebf\u7a0b\uff0c\u800c\u4e14\u5f53\u65f6\u56e0\u4e3a\u6570\u636e\u91cf\u5c11\uff0c\u6ca1\u6709\u51fa\u73b0\u8fc7\u591a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":378,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":930,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions\/930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/media\/378"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}