{"id":59222,"date":"2024-10-12T21:11:39","date_gmt":"2024-10-12T13:11:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59222\/"},"modified":"2024-10-12T21:11:39","modified_gmt":"2024-10-12T13:11:39","slug":"app%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e4%bf%ae%e6%94%b9%e6%96%87%e4%bb%b6%e6%ad%a5%e9%a9%9f%e8%88%87%e6%8f%90%e7%a4%ba-%e5%a6%82%e4%bd%95%e5%9c%a8app%e4%b8%8a%e4%bf%ae%e6%94%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59222\/","title":{"rendered":"App\u64cd\u4f5c\u6578\u64da\u5eab\uff1a\u4fee\u6539\u6587\u4ef6\u6b65\u9a5f\u8207\u63d0\u793a (\u5982\u4f55\u5728app\u4e0a\u4fee\u6539\u6578\u64da\u5eab\u6587\u4ef6\u5728\u54ea\u88e1\u770b)"},"content":{"rendered":"<h1 id=\"app%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e4%bf%ae%e6%94%b9%e6%96%87%e4%bb%b6%e6%ad%a5%e9%a9%9f%e8%88%87%e6%8f%90%e7%a4%ba-VoCAsgObzH\">App\u64cd\u4f5c\u6578\u64da\u5eab\uff1a\u4fee\u6539\u6587\u4ef6\u6b65\u9a5f\u8207\u63d0\u793a<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u8207\u64cd\u4f5c\u662f\u81f3\u95dc\u91cd\u8981\u7684\u4e00\u74b0\u3002\u7121\u8ad6\u662f\u79fb\u52d5\u61c9\u7528\u9084\u662f\u7db2\u9801\u61c9\u7528\uff0c\u6578\u64da\u5eab\u90fd\u627f\u64d4\u8457\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u7684\u91cd\u4efb\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u6587\u4ef6\u7684\u6b65\u9a5f\u8207\u63d0\u793a\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u7ba1\u7406\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-VoCAsgObzH\">\u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u7d44\u7e54\u7684\u6578\u64da\u96c6\u5408\uff0c\u901a\u5e38\u7528\u65bc\u5b58\u5132\u3001\u6aa2\u7d22\u548c\u7ba1\u7406\u6578\u64da\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b\u5305\u62ec\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982 MySQL\u3001PostgreSQL\uff09\u548c\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982 MongoDB\u3001Cassandra\uff09\u3002\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u901a\u5e38\u6d89\u53ca\u5230\u589e\u3001\u522a\u3001\u6539\u3001\u67e5\u7b49\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab%e6%96%87%e4%bb%b6%e7%9a%84%e6%ad%a5%e9%a9%9f-VoCAsgObzH\">\u4fee\u6539\u6578\u64da\u5eab\u6587\u4ef6\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e7%a2%ba%e8%aa%8d%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-VoCAsgObzH\">1. \u78ba\u8a8d\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u5728\u9032\u884c\u4efb\u4f55\u4fee\u6539\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u80fd\u5920\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u914d\u7f6e\u6578\u64da\u5eab\u7684\u9023\u63a5\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u4e3b\u6a5f\u540d\u3001\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<pre><code>const db = require('mysql').createConnection({\n    host: 'localhost',\n    user: 'yourUsername',\n    password: 'yourPassword',\n    database: 'yourDatabase'\n});<\/code><\/pre>\n<h3 id=\"2-%e7%b7%a8%e5%af%ab%e4%bf%ae%e6%94%b9%e8%aa%9e%e5%8f%a5-VoCAsgObzH\">2. \u7de8\u5beb\u4fee\u6539\u8a9e\u53e5<\/h3>\n<p>\u5728\u78ba\u4fdd\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u7de8\u5beb SQL \u8a9e\u53e5\u4f86\u4fee\u6539\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u66f4\u65b0\u7528\u6236\u4fe1\u606f\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>const updateUser = (userId, newName) =&gt; {\n    const sql = 'UPDATE users SET name = ? WHERE id = ?';\n    db.query(sql, [newName, userId], (error, results) =&gt; {\n        if (error) throw error;\n        console.log('\u7528\u6236\u4fe1\u606f\u5df2\u66f4\u65b0');\n    });\n};<\/code><\/pre>\n<h3 id=\"3-%e5%9f%b7%e8%a1%8c%e4%bf%ae%e6%94%b9%e6%93%8d%e4%bd%9c-VoCAsgObzH\">3. \u57f7\u884c\u4fee\u6539\u64cd\u4f5c<\/h3>\n<p>\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u57f7\u884c\u4fee\u6539\u64cd\u4f5c\uff0c\u4e26\u8655\u7406\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\u3002\u78ba\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u5f8c\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\u3002<\/p>\n<pre><code>db.end();<\/code><\/pre>\n<h2 id=\"%e6%8f%90%e7%a4%ba%e8%88%87%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-VoCAsgObzH\">\u63d0\u793a\u8207\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>\u5099\u4efd\u6578\u64da\uff1a<\/strong>\u5728\u9032\u884c\u4efb\u4f55\u4fee\u6539\u4e4b\u524d\uff0c\u52d9\u5fc5\u5099\u4efd\u6578\u64da\u5eab\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e8b\u52d9\uff1a<\/strong>\u5c0d\u65bc\u6d89\u53ca\u591a\u500b\u64cd\u4f5c\u7684\u4fee\u6539\uff0c\u5efa\u8b70\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u6e2c\u8a66\u4fee\u6539\uff1a<\/strong>\u5728\u751f\u7522\u74b0\u5883\u4e2d\u9032\u884c\u4fee\u6539\u4e4b\u524d\uff0c\u61c9\u5728\u6e2c\u8a66\u74b0\u5883\u4e2d\u9032\u884c\u5145\u5206\u6e2c\u8a66\u3002<\/li>\n<li><strong>\u65e5\u8a8c\u8a18\u9304\uff1a<\/strong>\u8a18\u9304\u6240\u6709\u7684\u4fee\u6539\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u65bc\u65e5\u5f8c\u7684\u67e5\u8a62\u548c\u8ffd\u8e64\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e6%96%87%e4%bb%b6-VoCAsgObzH\">\u5982\u4f55\u67e5\u770b\u6578\u64da\u5eab\u6587\u4ef6<\/h2>\n<p>\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u67e5\u770b\u6578\u64da\u5eab\u6587\u4ef6\u7684\u65b9\u5f0f\u53d6\u6c7a\u65bc\u6240\u4f7f\u7528\u7684\u6578\u64da\u5eab\u985e\u578b\u3002\u5c0d\u65bc\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 SQL \u67e5\u8a62\u4f86\u6aa2\u7d22\u6578\u64da\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>const getUser = (userId) =&gt; {\n    const sql = 'SELECT * FROM users WHERE id = ?';\n    db.query(sql, [userId], (error, results) =&gt; {\n        if (error) throw error;\n        console.log(results);\n    });\n};<\/code><\/pre>\n<p>\u5c0d\u65bc\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u5247\u53ef\u4ee5\u4f7f\u7528\u76f8\u61c9\u7684\u67e5\u8a62\u8a9e\u6cd5\u4f86\u6aa2\u7d22\u6578\u64da\u3002\u4e86\u89e3\u5982\u4f55\u67e5\u770b\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u6587\u4ef6\u662f\u958b\u767c\u8005\u5fc5\u5099\u7684\u6280\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VoCAsgObzH\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u6587\u4ef6\u662f\u4e00\u9805\u91cd\u8981\u7684\u4efb\u52d9\uff0c\u6d89\u53ca\u5230\u6578\u64da\u7684\u589e\u3001\u522a\u3001\u6539\u3001\u67e5\u7b49\u64cd\u4f5c\u3002\u901a\u904e\u6b63\u78ba\u7684\u6b65\u9a5f\u548c\u6ce8\u610f\u4e8b\u9805\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u8207\u4e00\u81f4\u6027\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 <a href=\"https:\/\/server.hk\">VPS<\/a> \u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728App\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u6587\u4ef6\uff0c\u6b65\u9a5f\u8207\u63d0\u793a\u4e00\u61c9\u4ff1\u5168\uff0c\u52a9\u60a8\u8f15\u9b06\u627e\u5230\u6240\u9700\u8cc7\u8a0a\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-59222","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59222","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=59222"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59222\/revisions"}],"predecessor-version":[{"id":59223,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59222\/revisions\/59223"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}