{"id":402,"date":"2019-02-28T16:26:26","date_gmt":"2019-02-28T08:26:26","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=402"},"modified":"2022-05-17T09:43:44","modified_gmt":"2022-05-17T01:43:44","slug":"python-3","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=402","title":{"rendered":"python\u751f\u6210bat\u811a\u672c\uff0c\u5e76\u4e14\u6267\u884cbat\u811a\u672c"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u5173\u952e\u8bcd\uff1apython,sqllite,bat,\u8fdb\u7a0b<br \/>\n\u6700\u8fd1\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u7528python\u8bfb\u53d6\u4e00\u4e2atxt\u6587\u4ef6\u91cc\u7684sql\u8bed\u53e5\uff0c\u7528sqllite\u53bb\u6267\u884c\uff0c\u6267\u884c\u4e4b\u524d\u5148\u5907\u4efd\u4e00\u6b21sqllite\u6570\u636e\u5e93\u5230\u4e34\u65f6\u76ee\u5f55\u3002\u7136\u540esql\u6267\u884c\u6210\u529f\u5219\u5220\u9664\u4e34\u65f6\u6587\u4ef6\uff0csql\u6267\u884c\u5931\u8d25\u5219\u4f7f\u7528\u5907\u4efd\u7684sqllite\u53bb\u8986\u76d6\u6389\u5f53\u524d\u9519\u8bef\u7684sqllite\u6570\u636e\u5e93\u3002<\/p>\n<h1>\u5173\u952e\u70b9<\/h1>\n<ul>\n<li>\u751f\u6210\u4e34\u65f6\u76ee\u5f55\u3002\u4f7f\u7528python\u7684tempfile\u5e93<\/li>\n<li>\u590d\u5236\u6587\u4ef6\uff0c\u4f7f\u7528python\u7684shutil\u5e93<\/li>\n<li>\u4ece\u6587\u672c\u4e2d\u8bfb\u53d6sql\uff0c\u4f7f\u7528open\u65b9\u6cd5:<\/li>\n<li>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">f = open(file_name, 'r')\nsql = f.read()<\/code><\/pre>\n<\/li>\n<li>\u751f\u6210bat\u6587\u4ef6\uff0c\u4e5f\u4f7f\u7528open\u65b9\u6cd5\u3002\u6a21\u5f0f\u9009\u62e9w<\/li>\n<li>bat\u547d\u4ee4\u4e2d\u7684\u6682\u505c\u4f7f\u7528@ping -n 5 127.1 &gt;nul 2&gt;nul<\/li>\n<li>bat\u547d\u4ee4\u4e2d\u7684\u5220\u9664\u4f7f\u7528\u201cdel \u8def\u5f84\u201d<\/li>\n<li>bat\u547d\u4ee4\u4e2d\u7684\u590d\u5236\u4f7f\u7528\u201ccopy \u6e90\u6587\u4ef6 \u76ee\u6807\u6587\u4ef6\u201d<\/li>\n<li>bat\u547d\u4ee4\u6267\u884c\u5b8c\u540e\uff0c\u7b49\u5f85\u7528\u6237\u624b\u52a8\u5173\u95ed\u4f7f\u7528pause\uff0c\u81ea\u52a8\u5173\u95ed\u4f7f\u7528exit<\/li>\n<\/ul>\n<h1>\u90e8\u5206\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 -*-\n# \u547d\u540d\u65b9\u5f0f\u4e3a\u8868\u540d_\u64cd\u4f5c_\u5b57\u6bb5\nimport os\nimport tempfile\nimport connectDB\nfrom controller import fileController\ncurosrdiction = connectDB.curosr_diction\ndatabase_back_folder = \"\"\ndef exec_sql(sqltext):\n    '''\n    \u6267\u884csql\uff0c\u6210\u529f\u8fd4\u56detrue\n    :param sqltext: sql\u8bed\u53e5\n    :return:\n    '''\n    result = curosrdiction.executescript(sqltext)\n    curosrdiction.commit()\n    return result\ndef backup_database():\n    '''\n    \u5907\u4efd\u6570\u636e\u5e93\n    :return:\n    '''\n    global database_back_folder\n    database_back_folder = tempfile.mkdtemp()\n    fileController.copy_file_to_folder('resource\/sqllite.db', database_back_folder)\n    write_bat()\ndef restore_database():\n    '''\n    \u6062\u590d\u6570\u636e\u5e93\n    :return:\n    '''\n    curosrdiction.close()\n    fileController.copy_file(srcfile=database_back_folder + \"\\\\sqllite.db\", dstfile='resource\/sqllite.db')\ndef read_sql_from_text(file_name):\n    '''\n    \u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6sql\u5e76\u4e14\u6267\u884c\uff0c\u6267\u884c\u5931\u8d25\uff0c\u5c31\u628a\u539f\u6765\u7684\u6570\u636e\u5e93\u8986\u76d6\u56de\u6765\n    :param file_name:\n    :return:\n    '''\n    f = open(file_name, 'r')\n    sql = f.read()\n    if not exec_sql(sql):\n        run_bat()\ndef write_bat():\n    sql_database_path = os.getcwd() + \"\\\\resource\\\\sqllite.db\"\n    sql_database_path_shm = os.getcwd() + \"\\\\resource\\\\sqllite.db-shm\"\n    sql_database_path_wal = os.getcwd() + \"\\\\resource\\\\sqllite.db-wal\"\n    bat_name = 'copy.bat'\n    s1 = '''@echo off\n@ping -n 5 127.1 &gt;nul 2&gt;nul\necho delete database...\ndel ''' + sql_database_path + '''\ndel ''' + sql_database_path_shm + '''\ndel ''' + sql_database_path_wal + '''\necho restore database...\n@ping -n 5 127.1 &gt;nul 2&gt;nul\ncopy ''' + database_back_folder + \"\\\\sqllite.db \" + sql_database_path + '''\necho restore finish\npause'''\n    f = open(bat_name, 'w')\n    f.write(s1)\n    f.close()\ndef run_bat():\n    '''\n    \u8fd0\u884cbat\n    :return:\n    '''\n    os.system('start copy.bat')\ndef update_sql():\n    '''\n    \u5f00\u59cb\u66f4\u65b0\u6570\u636e\u5e93\u7684\u4e3b\u5165\u53e3\n    :return:\n    '''\n    backup_database()\n    read_sql_from_text(\"resource\/download\/1.2.3_sql.txt\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u5173\u952e\u8bcd\uff1apython,sqllite,bat,\u8fdb\u7a0b \u6700\u8fd1\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u7528python\u8bfb\u53d6\u4e00\u4e2atxt\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-402","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/402","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=402"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":920,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/402\/revisions\/920"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}