{"id":58909,"date":"2024-10-12T21:07:25","date_gmt":"2024-10-12T13:07:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58909\/"},"modified":"2024-10-12T21:07:25","modified_gmt":"2024-10-12T13:07:25","slug":"api%e9%80%a3%e6%8e%a5sql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e6%a7%8b%e5%bb%ba%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e4%ba%a4%e4%ba%92%e5%b9%b3%e5%8f%b0-api%e9%80%a3%e6%8e%a5sql%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58909\/","title":{"rendered":"API\u9023\u63a5SQL\u6578\u64da\u5eab\uff0c\u69cb\u5efa\u9ad8\u6548\u6578\u64da\u4ea4\u4e92\u5e73\u53f0 (api\u9023\u63a5sql\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"api%e9%80%a3%e6%8e%a5sql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e6%a7%8b%e5%bb%ba%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e4%ba%a4%e4%ba%92%e5%b9%b3%e5%8f%b0-dAljbIjAMq\">API\u9023\u63a5SQL\u6578\u64da\u5eab\uff0c\u69cb\u5efa\u9ad8\u6548\u6578\u64da\u4ea4\u4e92\u5e73\u53f0<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cAPI\uff08\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\uff09\u5df2\u6210\u70ba\u4e0d\u540c\u7cfb\u7d71\u4e4b\u9593\u9032\u884c\u6578\u64da\u4ea4\u4e92\u7684\u95dc\u9375\u5de5\u5177\u3002\u7279\u5225\u662f\u5728\u9023\u63a5SQL\u6578\u64da\u5eab\u7684\u60c5\u5883\u4e0b\uff0cAPI\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u8a2a\u554f\u6027\uff0c\u9084\u80fd\u589e\u5f37\u61c9\u7528\u7a0b\u5f0f\u7684\u9748\u6d3b\u6027\u548c\u64f4\u5c55\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904eAPI\u9023\u63a5SQL\u6578\u64da\u5eab\uff0c\u4e26\u69cb\u5efa\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u4ea4\u4e92\u5e73\u53f0\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afapi%ef%bc%9f-dAljbIjAMq\">\u4ec0\u9ebc\u662fAPI\uff1f<\/h2>\n<p>API\u662f\u4e00\u7d44\u5b9a\u7fa9\u4e86\u4e0d\u540c\u8edf\u9ad4\u7d44\u4ef6\u4e4b\u9593\u5982\u4f55\u4e92\u52d5\u7684\u898f\u7bc4\u3002\u5b83\u5141\u8a31\u958b\u767c\u8005\u5728\u4e0d\u9700\u8981\u4e86\u89e3\u5167\u90e8\u5be6\u73fe\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u7279\u5b9a\u7684\u529f\u80fd\u6216\u6578\u64da\u3002API\u53ef\u4ee5\u662f\u7db2\u7d61API\u3001\u5eabAPI\u6216\u64cd\u4f5c\u7cfb\u7d71API\u7b49\uff0c\u9019\u88e1\u6211\u5011\u4e3b\u8981\u95dc\u6ce8\u7db2\u7d61API\uff0c\u7279\u5225\u662fRESTful API\u3002<\/p>\n<h2 id=\"sql%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a6%82%e8%bf%b0-dAljbIjAMq\">SQL\u6578\u64da\u5eab\u6982\u8ff0<\/h2>\n<p>SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u6578\u64da\u5eab\u662f\u7528\u65bc\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u7684\u7cfb\u7d71\u3002\u5e38\u898b\u7684SQL\u6578\u64da\u5eab\u5305\u62ecMySQL\u3001PostgreSQL\u548cMicrosoft SQL Server\u7b49\u3002\u9019\u4e9b\u6578\u64da\u5eab\u4f7f\u7528\u8868\u683c\u4f86\u7d44\u7e54\u6578\u64da\uff0c\u4e26\u652f\u6301\u8907\u96dc\u7684\u67e5\u8a62\u548c\u4e8b\u52d9\u8655\u7406\u3002<\/p>\n<h2 id=\"api%e9%80%a3%e6%8e%a5sql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-dAljbIjAMq\">API\u9023\u63a5SQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e8%a8%ad%e8%a8%88api%e7%b5%90%e6%a7%8b-dAljbIjAMq\">1. \u8a2d\u8a08API\u7d50\u69cb<\/h3>\n<p>\u5728\u958b\u59cb\u7de8\u78bc\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8a2d\u8a08API\u7684\u7d50\u69cb\u3002\u9019\u5305\u62ec\u78ba\u5b9aAPI\u7684\u7aef\u9ede\u3001\u8acb\u6c42\u65b9\u6cd5\uff08\u5982GET\u3001POST\u3001PUT\u3001DELETE\uff09\u4ee5\u53ca\u6240\u9700\u7684\u8acb\u6c42\u548c\u97ff\u61c9\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u8981\u5275\u5efa\u4e00\u500b\u7528\u65bc\u7372\u53d6\u7528\u6236\u4fe1\u606f\u7684API\u7aef\u9ede\uff1a<\/p>\n<pre><code>GET \/api\/users\/{id}<\/code><\/pre>\n<h3 id=\"2-%e9%81%b8%e6%93%87%e9%96%8b%e7%99%bc%e8%aa%9e%e8%a8%80%e5%92%8c%e6%a1%86%e6%9e%b6-dAljbIjAMq\">2. \u9078\u64c7\u958b\u767c\u8a9e\u8a00\u548c\u6846\u67b6<\/h3>\n<p>\u9078\u64c7\u5408\u9069\u7684\u958b\u767c\u8a9e\u8a00\u548c\u6846\u67b6\u5c0d\u65bcAPI\u7684\u958b\u767c\u81f3\u95dc\u91cd\u8981\u3002\u5e38\u898b\u7684\u9078\u64c7\u5305\u62ecNode.js\u3001Python\uff08Flask\u6216Django\uff09\u3001Java\uff08Spring Boot\uff09\u7b49\u3002\u9019\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u7c21\u5316API\u958b\u767c\u7684\u5de5\u5177\u548c\u5eab\u3002<\/p>\n<h3 id=\"3-%e9%80%a3%e6%8e%a5sql%e6%95%b8%e6%93%9a%e5%ba%ab-dAljbIjAMq\">3. \u9023\u63a5SQL\u6578\u64da\u5eab<\/h3>\n<p>\u5728API\u4e2d\u9023\u63a5SQL\u6578\u64da\u5eab\u901a\u5e38\u9700\u8981\u4f7f\u7528\u76f8\u61c9\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u548cMySQL\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const mysql = require('mysql');\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'yourUsername',\n    password: 'yourPassword',\n    database: 'yourDatabase'\n});\n\nconnection.connect((err) =&gt; {\n    if (err) throw err;\n    console.log('Connected to the database!');\n});\n<\/code><\/pre>\n<h3 id=\"4-%e5%af%a6%e7%8f%beapi%e9%82%8f%e8%bc%af-dAljbIjAMq\">4. \u5be6\u73feAPI\u908f\u8f2f<\/h3>\n<p>\u5728\u9023\u63a5\u6578\u64da\u5eab\u5f8c\uff0c\u60a8\u9700\u8981\u5be6\u73feAPI\u7684\u908f\u8f2f\u3002\u4f8b\u5982\uff0c\u7372\u53d6\u7528\u6236\u4fe1\u606f\u7684API\u53ef\u4ee5\u9019\u6a23\u5be6\u73fe\uff1a<\/p>\n<pre><code>app.get('\/api\/users\/:id', (req, res) =&gt; {\n    const userId = req.params.id;\n    connection.query('SELECT * FROM users WHERE id = ?', [userId], (error, results) =&gt; {\n        if (error) throw error;\n        res.json(results);\n    });\n});\n<\/code><\/pre>\n<h3 id=\"5-%e6%b8%ac%e8%a9%a6api-dAljbIjAMq\">5. \u6e2c\u8a66API<\/h3>\n<p>\u5728\u958b\u767c\u5b8c\u6210\u5f8c\uff0c\u4f7f\u7528Postman\u6216\u5176\u4ed6API\u6e2c\u8a66\u5de5\u5177\u9032\u884c\u6e2c\u8a66\uff0c\u78ba\u4fddAPI\u80fd\u5920\u6b63\u78ba\u5730\u8655\u7406\u8acb\u6c42\u4e26\u8fd4\u56de\u9810\u671f\u7684\u7d50\u679c\u3002<\/p>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-dAljbIjAMq\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u78ba\u4fddAPI\u7684\u5b89\u5168\u6027\uff0c\u4f7f\u7528\u8eab\u4efd\u9a57\u8b49\u548c\u6388\u6b0a\u6a5f\u5236\u4f86\u4fdd\u8b77\u6578\u64da\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5be6\u73fe\u826f\u597d\u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u8fd4\u56de\u6e05\u6670\u7684\u932f\u8aa4\u4fe1\u606f\u3002<\/li>\n<li><strong>\u6587\u6a94\uff1a<\/strong>\u70baAPI\u7de8\u5beb\u8a73\u7d30\u7684\u6587\u6a94\uff0c\u65b9\u4fbf\u5176\u4ed6\u958b\u767c\u8005\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-dAljbIjAMq\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904eAPI\u9023\u63a5SQL\u6578\u64da\u5eab\uff0c\u53ef\u4ee5\u69cb\u5efa\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u4ea4\u4e92\u5e73\u53f0\uff0c\u9019\u5c0d\u65bc\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u9078\u64c7\u5408\u9069\u7684\u958b\u767c\u8a9e\u8a00\u3001\u8a2d\u8a08API\u7d50\u69cb\uff0c\u9084\u662f\u78ba\u4fdd\u5b89\u5168\u6027\u548c\u826f\u597d\u7684\u932f\u8aa4\u8655\u7406\uff0c\u9019\u4e9b\u90fd\u662f\u6210\u529f\u7684\u95dc\u9375\u56e0\u7d20\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684API\u958b\u767c\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u901a\u904eAPI\u9023\u63a5SQL\u6578\u64da\u5eab\uff0c\u69cb\u5efa\u9ad8\u6548\u7684\u6578\u64da\u4ea4\u4e92\u5e73\u53f0\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u548c\u61c9\u7528\u6574\u5408\u80fd\u529b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58909","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58909","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58909"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58909\/revisions"}],"predecessor-version":[{"id":58910,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58909\/revisions\/58910"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}