{"id":20563,"date":"2024-11-19T09:27:10","date_gmt":"2024-11-19T01:27:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/20563\/"},"modified":"2024-11-19T09:27:10","modified_gmt":"2024-11-19T01:27:10","slug":"sublime%e6%80%8e%e4%b9%88%e7%94%a8mysql%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/20563\/","title":{"rendered":"sublime\u600e\u4e48\u7528mysql\u6570\u636e\u5e93"},"content":{"rendered":"<blockquote><p>\n  sublime text \u4e2d\u8fde\u63a5 mysql \u6570\u636e\u5e93\u7684\u6b65\u9aa4\uff1a\u5b89\u88c5 mysqldb \u6a21\u5757\u3002\u521b\u5efa\u4e00\u4e2a python \u811a\u672c\u5e76\u5bfc\u5165 mysql.connector\u3002\u901a\u8fc7 connect() \u65b9\u6cd5\u5efa\u7acb\u8fde\u63a5\u3002\u4f7f\u7528 cursor() \u65b9\u6cd5\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u5e76\u6267\u884c\u67e5\u8be2\u3002\u4f7f\u7528 fetchall() \u65b9\u6cd5\u68c0\u7d22\u67e5\u8be2\u7ed3\u679c\u3002\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u8fde\u63a5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202404\/03\/2024040305482062169.jpg\" class=\"aligncenter\" title=\"sublime\u600e\u4e48\u7528mysql\u6570\u636e\u5e93\u63d2\u56fe\" alt=\"sublime\u600e\u4e48\u7528mysql\u6570\u636e\u5e93\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Sublime Text \u8fde\u63a5 MySQL \u6570\u636e\u5e93<\/h2>\n<h3>\u5f00\u95e8\u89c1\u5c71<\/h3>\n<p>Sublime Text \u662f\u4e00\u6b3e\u6d41\u884c\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fde\u63a5\u81f3 MySQL \u6570\u636e\u5e93\uff1a<\/p>\n<h3>\u6b65\u9aa4\u8be6\u89e3<\/h3>\n<h4>1. \u5b89\u88c5 MySQLdb \u6a21\u5757<\/h4>\n<p>\u8981\u8fde\u63a5 Python \u4e2d\u7684 MySQL\uff0c\u9700\u8981\u5b89\u88c5 MySQLdb \u6a21\u5757\uff1a<\/p>\n<pre>pip install mysqlclient<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4>2. \u521b\u5efa\u4e00\u4e2a Python \u811a\u672c<\/h4>\n<p>\u5728 Sublime Text \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a connect_mysql.py\uff1a<\/p>\n<pre>import mysql.connector\n\n# localhost \u662f MySQL \u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u4e3b\u673a\u540d\n# \u7aef\u53e3\u53f7\u901a\u5e38\u4e3a 3306\n# \u7528\u6237\u540d\u548c\u5bc6\u7801\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7684\u914d\u7f6e\nmydb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"username\",\n    password=\"password\",\n    database=\"database_name\"\n)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4>3. \u6267\u884c\u67e5\u8be2<\/h4>\n<p>\u8981\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 cursor() \u65b9\u6cd5\u83b7\u53d6\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff1a<\/p>\n<pre>mycursor = mydb.cursor()\nmycursor.execute(\"SELECT * FROM table_name\")<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4>4. \u68c0\u7d22\u7ed3\u679c<\/h4>\n<p>\u8981\u68c0\u7d22\u67e5\u8be2\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528 fetchall() \u65b9\u6cd5\uff1a<\/p>\n<pre>results = mycursor.fetchall()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4>5. \u5173\u95ed\u8fde\u63a5<\/h4>\n<p>\u5b8c\u6210\u540e\uff0c\u8bf7\u52a1\u5fc5\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<pre>mycursor.close()\nmydb.close()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u786e\u4fdd MySQL \u670d\u52a1\u5668\u6b63\u5728\u8fd0\u884c\u3002<\/li>\n<li>\u786e\u4fdd\u7528\u6237\u540d\u548c\u5bc6\u7801\u6b63\u786e\u65e0\u8bef\u3002<\/li>\n<li>\u5982\u679c\u8fde\u63a5\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u9632\u706b\u5899\u8bbe\u7f6e\u3002<\/li>\n<li>Sublime Text \u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u63d2\u4ef6\uff08\u4f8b\u5982 Database Package\uff09\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528mysql\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sublime text \u4e2d\u8fde\u63a5 mysql \u6570\u636e\u5e93\u7684\u6b65\u9aa4\uff1a\u5b89\u88c5 mysqldb \u6a21\u5757\u3002\u521b\u5efa\u4e00\u4e2a python \u811a\u672c\u5e76\u5bfc\u5165 mysql.connector\u3002\u901a\u8fc7 connect() \u65b9\u6cd5\u5efa\u7acb\u8fde\u63a5\u3002\u4f7f\u7528 cursor() \u65b9\u6cd5\u521b\u5efa\u6e38\u6807\u5bf9\u8c61\u5e76\u6267\u884c\u67e5\u8be2\u3002\u4f7f\u7528 fetchall() \u65b9\u6cd5\u68c0\u7d22\u67e5\u8be2\u7ed3\u679c\u3002\u4f7f\u7528 close() \u65b9\u6cd5\u5173\u95ed\u8fde\u63a5\u3002 \u5982\u4f55\u4f7f\u7528 Sublime Text \u8fde\u63a5 MySQL \u6570\u636e\u5e93 \u5f00\u95e8\u89c1\u5c71 Sublime Text \u662f\u4e00\u6b3e\u6d41\u884c\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fde\u63a5\u81f3 MySQL \u6570\u636e\u5e93\uff1a \u6b65\u9aa4\u8be6\u89e3 1. \u5b89\u88c5 MySQLdb \u6a21\u5757 \u8981\u8fde\u63a5 Python \u4e2d\u7684 MySQL\uff0c\u9700\u8981\u5b89\u88c5 MySQLdb \u6a21\u5757\uff1a pip install mysqlclient \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u4e00\u4e2a Python \u811a\u672c \u5728 Sublime Text \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a connect_mysql.py\uff1a import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-20563","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/20563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=20563"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/20563\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=20563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=20563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=20563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}