{"id":1033,"date":"2022-11-14T09:26:31","date_gmt":"2022-11-14T01:26:31","guid":{"rendered":"https:\/\/blog.langsasec.cn\/?p=1033"},"modified":"2022-11-14T09:26:45","modified_gmt":"2022-11-14T01:26:45","slug":"pyrmsjkbc","status":"publish","type":"post","link":"https:\/\/blog.langsasec.cn\/index.php\/2022\/11\/14\/pyrmsjkbc\/","title":{"rendered":"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929"},"content":{"rendered":"<p>\u9996\u5148\uff0c\u6211\u4eec\u5148\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f\u6570\u636e\u5e93\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u6570\u636e\u5e93\uff1f<\/p>\n<p>\u6570\u636e\u5e93\uff0c\u7b80\u5355\u7684\u8bf4\u5c31\u662f\u5b58\u50a8\u6570\u636e\u7684\u4ed3\u5e93\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u6570\u636e\u5e93\uff1f\u7b80\u5355\u7684\u8bf4\u5c31\u662f\u66f4\u597d\u7684\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u4ee5\u4e0a\u66f4\u52a0\u8be6\u7ec6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u76f4\u63a5\u767e\u5ea6\uff0c\u6216\u8005\u6709\u9700\u8981\u7684\u8bdd\u5728\u4e0b\u9762\u8fdb\u884c\u8bc4\u8bba\uff0c\u6211\u4f1a\u7ed9\u5927\u5bb6\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n<p>OK\uff0c\u8fdb\u5165\u6211\u4eec\u7684\u4e3b\u9898\uff0c\u548c\u6570\u636e\u5e93\u804a\u5929\uff0c\u8be5\u7bc7\u4e3b\u8981\u5c06\u7684\u662f\u4f7f\u7528Python\u548c\u6570\u636e\u5e93\u8fdb\u884c\u201c\u804a\u5929\u201d\u3002<\/p>\n<p>Python\u6570\u636e\u5e93\u63a5\u53e3\u652f\u6301\u975e\u5e38\u591a\u7684\u6570\u636e\u5e93\uff0c\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u7684\u662f\u4e0eMySQL\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff0c\u5176\u4ed6\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u5927\u5bb6\u53ef\u4ee5\u7528\u5982\u4e0b\u7684\u65b9\u6cd5\u7ee7\u7eed\u5b66\u4e60\u3002\u4f7f\u7528\u7684\u662fPython\u7684MySQLdb\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u662f\u7b2c\u4e09\u65b9\u6a21\u5757\u9700\u8981\u8fdb\u884c\u4e0b\u8f7d\uff0c\u4e0b\u8f7d\u65b9\u6cd5\u5982\u4e0b\uff0c<\/p>\n<p>\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8f93\u5165\uff1apip install mysql<\/p>\n<p>\u7136\u540e\uff0c\u8fdb\u884c\u672c\u5730\u6d4b\u8bd5\u9700\u8981\u4e0b\u8f7dMySQL\u73af\u5883\uff0c\u4f20\u7edf\u7684\u65b9\u5f0f\u6bd4\u8f83\u590d\u6742\uff0c\u63a8\u8350\u5927\u5bb6\u53ef\u4ee5\u76f4\u63a5\u4e0b\u8f7dphpstudy\uff0c\u6211\u8fd9\u8fb9\u662f\u4e0b\u8f7d\u4e86phpstudy_pro<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/1854a0631406e191013539858c0f072a8da2c4ce.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe\" \/> <\/p>\n<p>\u6253\u5f00\u4e4b\u540e\uff0c\u6211\u4eec\u542f\u52a8\u4e00\u4e0bmysql\u670d\u52a1\u5c31\u884c\u4e86<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/53c292065f2b7be1199309a76a4679c4b32790c3.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe1\" \/> <\/p>\n<p>\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u8f93\u5165mysql --version\uff0c\u67e5\u770b\u7248\u672c\u786e\u8ba4\u6211\u4eec\u7684MySQL\u670d\u52a1\u5df2\u7ecf\u542f\u52a8<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/3bacf9902872a35a3fdeadb238200017eebb7969.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe2\" \/> <\/p>\n<p>\u4e00\u5f00\u59cb\u6ca1\u6709\u6570\u636e\u5e93\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u521b\u5efa<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/181d8c394f10fa4744e53374d077b674bd087bca.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe3\" \/> <\/p>\n<p>\u547d\u4ee4\u884c\u4e2d\u6211\u4eec\u5c31\u53ef\u4ee5\u8f93\u5165\uff0cmysql -uroot -p123456\u8fdb\u884c\u767b\u5f55\uff0croot\u662f\u8d26\u53f7\uff0c123456\u662f\u5bc6\u7801\u3002\u5c31\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u767b\u5f55\u6210\u529f\u7684\u9875\u9762<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/af2d81a2154433a68a93ff84cc2d5f0f28729e66.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe4\" \/> <\/p>\n<p>\u4f7f\u7528show databases;\u547d\u4ee4\u5c31\u53ef\u4ee5\u67e5\u770b\u6211\u4eec\u6709\u54ea\u4e9b\u6570\u636e\u5e93\uff0c\u60c5\u51b5\u5982\u4e0b\uff0c\u7531\u4e8e\u6211\u5df2\u7ecf\u5148\u524d\u5c31\u521b\u5efa\u4e86\u90a3\u4e48\u591a\u7684\u6570\u636e\u5e93\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e00\u4e00\u663e\u793a\u51fa\u6765\uff0c\u6ca1\u6709\u6570\u636e\u5e93\u7684\u53ef\u4ee5\u901a\u8fc7create database <dbname> \u8fdb\u884c\u521b\u5efa\u6570\u636e\u5e93\uff0c\u7136\u540e\u518d\u8fdb\u884c\u67e5\u770b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/67521694490d2126dd3de28cba886dea2761cfa4.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe5\" \/><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/eaf5040cc3ac8f5c0f65b09879054cd628fc9bdf.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe6\" \/> <\/p>\n<p>\u592a\u591a\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u8fd9\u8fb9\u5c31\u5148\u4e0d\u8bb2\u4e86\u3002\u6211\u4eec\u7684\u4e3b\u9898\u8fd8\u662f\u8bf4\u901a\u8fc7Python\u6765\u64cd\u4f5c\u8fd9\u4e2a\u6570\u636e\u5e93\uff0c\u60f3\u4e86\u89e3\u66f4\u591a\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u65b9\u5f0f\u53ef\u4ee5\u901a\u8fc7\u767e\u5ea6\u8fdb\u884c\u4e86\u89e3\uff0c\u5982\u679c\u559c\u6b22\u5c0f\u7f16\u7684\u6559\u5b66\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4ee5\u4e0b\u8fdb\u884c\u8bc4\u8bba\u7559\u8a00\u201c<strong><em>*\u8fd8\u662f\u5c0f\u7f16\u8bb2\u5f97\u597d*<\/em><\/strong>\u201d\uff0c\u6211\u6839\u636e\u9700\u6c42\u60c5\u51b5\u9009\u62e9\u662f\u5426\u7ed9\u5927\u5bb6\u8bb2\u89e3\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<p>\u597d\u7684\uff0c\u7ee7\u7eed\u56de\u5f52\u6211\u4eec\u7684\u4e3b\u9898\u4e0a\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u8981\u5b8c\u6210\u7684\u4e8b\u60c5\u5c31\u662f\u901a\u8fc7Python\u7684\u65b9\u5f0f\u6765\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u6211\u4eec\u4ee5\u4e0a\u7684\u64cd\u4f5c\uff0c\u56de\u987e\u4e00\u4e0b\u6211\u4eec\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>1\u3001\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5<\/p>\n<p>2\u3001\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u4fe1\u606f<\/p>\n<p>3\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93<\/p>\n<p>4\u3001\u518d\u6b21\u67e5\u8be2\u6211\u4eec\u6570\u636e\u5e93\u7684\u4fe1\u606f<\/p>\n<p>\u6211\u4eec\u63a5\u4e0b\u6765\u5c31\u4f1a\u5c06\u4ee5\u4e0a\u7684\u64cd\u4f5c\u8fc7\u7a0b\u7528Python\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5<\/p>\n<p>\u7b2c\u4e00\u6b65\uff0c\u5bfc\u5165\u6a21\u5757<\/p>\n<p>Import MySQLdb<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff0c\u5efa\u7acb\u8fde\u63a5<\/p>\n<p>\u4f7f\u7528MySQL.connect(\u2018ip\u2019,\u2019username\u2019,\u2019password\u2019,charset=\u2019utf8\u2019)\u65b9\u6cd5\u5efa\u7acb\u8fde\u63a5<\/p>\n<p>\u7b2c\u4e09\u6b65\uff0c\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807<\/p>\n<p>\u4f7f\u7528cursor()\u65b9\u6cd5<\/p>\n<p>\u7b2c\u56db\u6b65\uff0c\u6267\u884cSQL\u8bed\u53e5<\/p>\n<p>\u4f7f\u7528execute()\u65b9\u6cd5\uff0c\u5982\u5148\u6267\u884cshow databases\u67e5\u770b\u6570\u636e\u5e93\uff0c\u7136\u540e\u521b\u5efa\u65b0\u6570\u636e\u5e93new_database\uff0c\u7136\u540e\u518dshow databases\u3002<\/p>\n<p>\u7b2c\u4e94\u6b65\uff0c\u83b7\u53d6\u6267\u884c\u7ed3\u679c<\/p>\n<p>\u4f7f\u7528fetchone()\u53ef\u4ee5\u83b7\u53d6\u4e00\u6761\u6267\u884c\u7ed3\u679c\uff0cfetchall()\u83b7\u53d6\u5168\u90e8\u7ed3\u679c\uff0c\u6211\u4eec\u76f4\u63a5\u83b7\u53d6\u5168\u90e8\u7ed3\u679c\u3002<\/p>\n<p>\u7b2c\u516d\u6b65\uff0c\u5173\u95ed\u6570\u636e\u5e93<\/p>\n<p>\u6211\u4eec\u518d\u4f7f\u7528\u5b8c\u6570\u636e\u5e93\u4e4b\u540e\u8981\u4f7f\u7528close()\u65b9\u6cd5\uff0c\u5173\u95ed\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u4ee5\u4e0a\u7684\u5185\u5bb9\uff0c\u6211\u4eec\u5f00\u59cb\u5b9e\u9645\u64cd\u4f5c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/fe56791490e76365633fb50789906a4c08e17963.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe7\" \/> <\/p>\n<p>\u4e00\u4e0b\u4e24\u6bb5\u5c31\u662f\u6211\u4eec\u6253\u5370\u7684\u4e24\u6b21\u7ed3\u679c\uff0c\u53ef\u4ee5\u770b\u51fa\u6211\u4eec\u786e\u5b9e\u5efa\u7acb\u8fde\u63a5\uff0c\u540c\u65f6\u6267\u884c\u4e86sql\u8bed\u53e5\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93new_database\u3002\u8ba9\u6211\u4eec\u901a\u8fc7\u547d\u4ee4\u884c\u53bb\u67e5\u770b\u4e00\u4e0b\u6267\u884c\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/f324282bae0bcfb1ce32669acab3af202e93423d.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe8\" \/> <\/p>\n<p>\u786e\u5b9e\u5df2\u7ecf\u521b\u5efa\u4e86\u6211\u4eec\u901a\u8fc7Python\u7f16\u7a0b\u521b\u5efa\u7684\u65b0\u6570\u636e\u5e93\uff0c\u5bf9\u4e8e\u6570\u636e\u5e93\u7684\u5176\u4ed6\u64cd\u4f5c\u90fd\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0a\u7684\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff0c\u6211\u4eec\u53d1\u73b0\u8fd9\u4e2a\u6253\u5370\u7684\u7ed3\u679c\u5f88\u7cd9\uff0c\u770b\u8d77\u6765\u5f88\u4e0d\u8212\u670d\u3002\u53ef\u4ee5\u901a\u8fc7tabulate\u6a21\u5757\u8fdb\u884c\u4f18\u5316\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/497a8371ac4533ab137370c8509dc4e328f20538.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe9\" \/> <\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<p>\u4e0b\u8f7d\u6a21\u5757\uff1apip install tabulate<\/p>\n<p>\u8f7d\u5165\u6a21\u5757\uff1afrom tabulate import tabulate<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a\u6253\u5370\u7684\u65f6\u5019\u4f7f\u7528tabulate()\u65b9\u6cd5\u5bf9SQL\u6267\u884c\u7ed3\u679c\u8fdb\u884c\u5904\u7406\uff0c\u518d\u8fdb\u884c\u6253\u5370\u3002<\/p>\n<p>\u4f7f\u7528headers\u8bbe\u7f6e\u8868\u5934\uff0c\u4f7f\u7528tablefmt\u8bbe\u7f6e\u98ce\u683c\uff0c\u5176\u4e2dpsql\u7684\u98ce\u683c\u521a\u597d\u548c\u547d\u4ee4\u884c\u7684\u6253\u5370\u7ed3\u679c\u7c7b\u4f3c\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.hdslb.com\/bfs\/album\/47c29ce9d0be1d5ae06b6c5a22f1fa2088f1e9a3.jpg\" alt=\"img\" title=\"\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u548c\u6570\u636e\u5e93\u804a\u5929\u63d2\u56fe10\" \/> <\/p>\n<p>OK\uff0c\u672c\u7bc7\u5185\u5bb9\u5230\u6b64\u4e3a\u6b62\uff0c\u60f3\u5b66\u4e60\u66f4\u591a\u5185\u5bb9\u53ef\u5173\u6ce8\u4e00\u4e0b\u5c0f\u7f16\u3002\u3010Python \u201c\u5077\u61d2\u201d\u5165\u95e8\u4e13\u680f\u3011\u4e5f\u544a\u4e00\u6bb5\u843d\u4e86\u3002\u540e\u7eed\u5c06\u63a8\u51fa\u5176\u4ed6\u5185\u5bb9\u7684\u5b66\u4e60\uff0c\u4e5f\u4f1a\u51fa\u4e00\u4e9b\u201c\u5077\u61d2\u201d\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u5e0c\u671b\u540e\u9762\u8fd8\u80fd\u770b\u5230\u5404\u4f4d\u7684\u70b9\u8d5e\u3001\u8bc4\u8bba\u3002\u611f\u8c22\uff01<\/p>\n<p>TO BE CONTINUE~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\uff0c\u6211\u4eec\u5148\u4e86\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f\u6570\u636e\u5e93\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u6570\u636e\u5e93\uff1f \u6570\u636e\u5e93\uff0c\u7b80\u5355\u7684\u8bf4\u5c31\u662f\u5b58\u50a8\u6570\u636e\u7684\u4ed3\u5e93\u3002 \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1033","post","type-post","status-publish","format-standard","hentry","category-aqkf"],"_links":{"self":[{"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1033"}],"version-history":[{"count":1,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1034,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/posts\/1033\/revisions\/1034"}],"wp:attachment":[{"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.langsasec.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}