{"id":171639,"date":"2024-11-07T04:25:17","date_gmt":"2024-11-06T20:25:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171639\/"},"modified":"2024-11-07T04:25:18","modified_gmt":"2024-11-06T20:25:18","slug":"%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7mysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%89%8d%e7%ab%af%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e6%9b%b4%e9%ab%98%e6%95%88%ef%bc%81-js%e8%a8%aa%e5%95%8fmysql","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171639\/","title":{"rendered":"\u8f15\u9b06\u64cd\u63a7MySQL\u6578\u64da\u5eab\uff0c\u524d\u7aef\u6578\u64da\u8655\u7406\u66f4\u9ad8\u6548\uff01 (js\u8a2a\u554fmysql\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7mysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%89%8d%e7%ab%af%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e6%9b%b4%e9%ab%98%e6%95%88%ef%bc%81-js%e8%a8%aa%e5%95%8fmysql-XPibHxpmrf\">\u8f15\u9b06\u64cd\u63a7MySQL\u6578\u64da\u5eab\uff0c\u524d\u7aef\u6578\u64da\u8655\u7406\u66f4\u9ad8\u6548\uff01 (js\u8a2a\u554fmysql\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002MySQL\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u96a8\u8457\u524d\u7aef\u6280\u8853\u7684\u767c\u5c55\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u958b\u767c\u8005\u5e0c\u671b\u80fd\u5920\u76f4\u63a5\u5f9e\u524d\u7aef\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff0c\u4ee5\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528JavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87javascript%e8%a8%aa%e5%95%8fmysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-XPibHxpmrf\">\u70ba\u4ec0\u9ebc\u9078\u64c7JavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff1f<\/h2>\n<p>JavaScript\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u7279\u5225\u662f\u5728\u524d\u7aef\u958b\u767c\u4e2d\u3002\u4f7f\u7528JavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5373\u6642\u6027\uff1a<\/strong>\u524d\u7aef\u53ef\u4ee5\u5373\u6642\u7372\u53d6\u6578\u64da\uff0c\u7121\u9700\u91cd\u65b0\u52a0\u8f09\u9801\u9762\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<li><strong>\u7c21\u5316\u6d41\u7a0b\uff1a<\/strong>\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u5728\u524d\u7aef\u8655\u7406\u6578\u64da\uff0c\u6e1b\u5c11\u5f8c\u7aef\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>JavaScript\u7684\u9748\u6d3b\u6027\u4f7f\u5f97\u6578\u64da\u8655\u7406\u548c\u5c55\u793a\u8b8a\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%bejavascript%e8%a8%aa%e5%95%8fmysql%e6%95%b8%e6%93%9a%e5%ba%ab-XPibHxpmrf\">\u5982\u4f55\u5be6\u73feJavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab<\/h2>\n<p>\u76f4\u63a5\u5f9e\u524d\u7aef\u8a2a\u554fMySQL\u6578\u64da\u5eab\u4e26\u4e0d\u5b89\u5168\uff0c\u56e0\u6b64\u901a\u5e38\u9700\u8981\u901a\u904e\u5f8c\u7aefAPI\u4f86\u9032\u884c\u6578\u64da\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u5be6\u73fe\u9019\u4e00\u904e\u7a0b\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e5%be%8c%e7%ab%afapi-XPibHxpmrf\">1. \u8a2d\u7f6e\u5f8c\u7aefAPI<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e\u4e00\u500b\u5f8c\u7aef\u670d\u52d9\u4f86\u8655\u7406\u524d\u7aef\u7684\u8acb\u6c42\u3002\u9019\u88e1\u4ee5Node.js\u548cExpress\u70ba\u4f8b\uff1a<\/p>\n<pre><code>const express = require('express');\nconst mysql = require('mysql');\nconst app = express();\nconst port = 3000;\n\n\/\/ \u5275\u5efaMySQL\u9023\u63a5\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'your_username',\n    password: 'your_password',\n    database: 'your_database'\n});\n\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\nconnection.connect((err) =&gt; {\n    if (err) throw err;\n    console.log('Connected to MySQL Database!');\n});\n\n\/\/ \u8a2d\u7f6eAPI\u8def\u7531\napp.get('\/api\/data', (req, res) =&gt; {\n    connection.query('SELECT * FROM your_table', (err, results) =&gt; {\n        if (err) throw err;\n        res.json(results);\n    });\n});\n\n\/\/ \u555f\u52d5\u670d\u52d9\napp.listen(port, () =&gt; {\n    console.log(`Server running at http:\/\/localhost:${port}`);\n});<\/code><\/pre>\n<h3 id=\"2-%e5%9c%a8%e5%89%8d%e7%ab%af%e4%bd%bf%e7%94%a8javascript%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-XPibHxpmrf\">2. \u5728\u524d\u7aef\u4f7f\u7528JavaScript\u7372\u53d6\u6578\u64da<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u524d\u7aef\u4f7f\u7528JavaScript\u4f86\u8a2a\u554f\u9019\u500bAPI\u4e26\u7372\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>fetch('http:\/\/localhost:3000\/api\/data')\n    .then(response =&gt; response.json())\n    .then(data =&gt; {\n        console.log(data);\n        \/\/ \u5728\u9019\u88e1\u8655\u7406\u6578\u64da\uff0c\u4f8b\u5982\u986f\u793a\u5728\u7db2\u9801\u4e0a\n    })\n    .catch(error =&gt; console.error('Error fetching data:', error));<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-XPibHxpmrf\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u5be6\u73feJavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab\u7684\u904e\u7a0b\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u8003\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u74b0\u5883\u8b8a\u91cf\uff1a<\/strong>\u4e0d\u8981\u5728\u4ee3\u78bc\u4e2d\u786c\u7de8\u78bc\u6578\u64da\u5eab\u6191\u8b49\uff0c\u61c9\u4f7f\u7528\u74b0\u5883\u8b8a\u91cf\u4f86\u4fdd\u8b77\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<li><strong>\u9a57\u8b49\u8acb\u6c42\uff1a<\/strong>\u78ba\u4fddAPI\u8acb\u6c42\u7d93\u904e\u9069\u7576\u7684\u9a57\u8b49\uff0c\u4ee5\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u3002<\/li>\n<li><strong>\u9632\u6b62SQL\u6ce8\u5165\uff1a<\/strong>\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XPibHxpmrf\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528JavaScript\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5be6\u73fe\u66f4\u9ad8\u6548\u7684\u524d\u7aef\u6578\u64da\u8655\u7406\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u7136\u800c\uff0c\u5b89\u5168\u6027\u554f\u984c\u4e0d\u5bb9\u5ffd\u8996\uff0c\u5fc5\u9808\u63a1\u53d6\u9069\u7576\u7684\u63aa\u65bd\u4f86\u4fdd\u8b77\u6578\u64da\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u61c9\u7528\u74b0\u5883\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u548c\u9ad8\u6548\u8655\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u64cd\u63a7MySQL\u6578\u64da\u5eab\uff0c\u63d0\u5347\u524d\u7aef\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u5be6\u73fe\u5feb\u901f\u8a2a\u554f\u8207\u7ba1\u7406\uff0c\u8b93\u958b\u767c\u66f4\u7c21\u55ae\uff01<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-171639","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=171639"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171639\/revisions"}],"predecessor-version":[{"id":171640,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171639\/revisions\/171640"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}