{"id":7559,"date":"2024-11-14T15:56:57","date_gmt":"2024-11-14T07:56:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7559\/"},"modified":"2024-11-14T15:56:57","modified_gmt":"2024-11-14T07:56:57","slug":"%e6%95%b0%e6%8d%ae%e5%ba%93%e6%8a%a5%e9%94%99unknown-database%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84sql%e8%af%ad%e5%8f%a5%e6%97%a0%e6%b3%95%e6%89%be%e5%88%b0%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7559\/","title":{"rendered":"\u6570\u636e\u5e93\u62a5\u9519\u201cUnknown database\u201d\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684SQL\u8bed\u53e5\u65e0\u6cd5\u627e\u5230\u76ee\u6807\u6570\u636e\u5e93\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173098784488911.jpg\" class=\"aligncenter\" title=\"\u6570\u636e\u5e93\u62a5\u9519\u201cUnknown database\u201d\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684SQL\u8bed\u53e5\u65e0\u6cd5\u627e\u5230\u76ee\u6807\u6570\u636e\u5e93\uff1f\u63d2\u56fe\" alt=\"\u6570\u636e\u5e93\u62a5\u9519\u201cUnknown database\u201d\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684SQL\u8bed\u53e5\u65e0\u6cd5\u627e\u5230\u76ee\u6807\u6570\u636e\u5e93\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\u7684\u5e38\u89c1\u9519\u8bef\uff1a\u672a\u6307\u5b9a\u6570\u636e\u5e93<\/strong><\/p>\n<p>\u5982\u95ee\u9898\u6240\u793a\uff0c\u7a0b\u5e8f\u5728\u6267\u884c sql \u67e5\u8be2\u65f6\u9047\u5230\uff1a&#8221;er_bad_db_error: unknown database&#8221; \u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a sql \u8bed\u53e5\u4e2d\u672a\u6307\u5b9a\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u65e0\u6cd5\u627e\u5230\u5bf9\u5e94\u7684\u8868\u3002<\/p>\n<p>\u5728  \u4e2d\uff0c\u8981\u6307\u5b9a\u6570\u636e\u5e93\uff0c\u9700\u8981\u5728 sql \u8bed\u53e5\u7684\u5f00\u5934\u6dfb\u52a0 use \u6570\u636e\u5e93\u540d \u8bed\u53e5\u3002\u4f8b\u5982\uff0c<\/p>\n<pre>use my_db_01;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u6b64\u8bed\u53e5\u540e\uff0c\u6240\u6709\u540e\u7eed\u7684 sql \u8bed\u53e5\u5c06\u9488\u5bf9\u6570\u636e\u5e93 my_db_01 \u6267\u884c\u3002<\/p>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 use \u8bed\u53e5\u6307\u5b9a\u6570\u636e\u5e93\uff0c\u7136\u540e\u6267\u884c sql \u67e5\u8be2\u3002\u4f8b\u5982\uff0c\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>exports.regUser = (req, res) =&gt; {\n  \/\/ 1. \u83b7\u53d6\u7528\u6237\u4fe1\u606f\n  const userinfo = req.body;\n\n  \/\/ 2. \u6307\u5b9a\u6570\u636e\u5e93\n  db.query('USE my_db_01;');\n\n  \/\/ 3. \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u67e5\u91cd\u7528\u6237\u540d\n  \/\/ 4. \u5b9a\u4e49 SQL \u8bed\u53e5\n  const sql = 'SELECT * FROM getloonwh_users WHERE username=? AND usernumber=?';\n\n  \/\/ 5. \u6267\u884c\n  db.query(sql, [userinfo.username, userinfo.usernumber], (err, results) =&gt; {\n    if (err) return res.cc(err);\n    if (results.length &gt; 0) return res.send('\u5df2\u5b58\u5728\u7528\u6237\u540d\uff01\u91cd\u65b0\u8f93\u5165');\n\n    const sqlStr = 'INSERT INTO getloonwh_users SET ?';\n    db.query(sqlStr, { username: userinfo.username, usernumber: userinfo.usernumber }, (err, results) =&gt; {\n      if (err) return res.cc(err);\n      if (results.affectedRows !== 1) return res.cc('\u6ce8\u518c\u5931\u8d25\uff01');\n      res.send({ status: 0, msg: '\u6ce8\u518c\u6210\u529f\uff01' });\n    });\n  });\n\n  \/\/ res.send('\u6ce8\u518c\u6210\u529f\uff01')\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u62a5\u9519\u201cUnknown database\u201d\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684SQL\u8bed\u53e5\u65e0\u6cd5\u627e\u5230\u76ee\u6807\u6570\u636e\u5e93\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\u7684\u5e38\u89c1\u9519\u8bef\uff1a\u672a\u6307\u5b9a\u6570\u636e\u5e93 \u5982\u95ee\u9898\u6240\u793a\uff0c\u7a0b\u5e8f\u5728\u6267\u884c sql \u67e5\u8be2\u65f6\u9047\u5230\uff1a&#8221;er_bad_db_error: unknown database&#8221; \u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a sql \u8bed\u53e5\u4e2d\u672a\u6307\u5b9a\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u65e0\u6cd5\u627e\u5230\u5bf9\u5e94\u7684\u8868\u3002 \u5728 \u4e2d\uff0c\u8981\u6307\u5b9a\u6570\u636e\u5e93\uff0c\u9700\u8981\u5728 sql \u8bed\u53e5\u7684\u5f00\u5934\u6dfb\u52a0 use \u6570\u636e\u5e93\u540d \u8bed\u53e5\u3002\u4f8b\u5982\uff0c use my_db_01; \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u6b64\u8bed\u53e5\u540e\uff0c\u6240\u6709\u540e\u7eed\u7684 sql \u8bed\u53e5\u5c06\u9488\u5bf9\u6570\u636e\u5e93 my_db_01 \u6267\u884c\u3002 \u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 use \u8bed\u53e5\u6307\u5b9a\u6570\u636e\u5e93\uff0c\u7136\u540e\u6267\u884c sql \u67e5\u8be2\u3002\u4f8b\u5982\uff0c\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a exports.regUser = (req, res) =&gt; { \/\/ 1. \u83b7\u53d6\u7528\u6237\u4fe1\u606f const userinfo = req.body; \/\/ 2. \u6307\u5b9a\u6570\u636e\u5e93 db.query(&#8216;USE my_db_01;&#8217;); \/\/ 3. \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u67e5\u91cd\u7528\u6237\u540d \/\/ 4. \u5b9a\u4e49 SQL \u8bed\u53e5 [&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-7559","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7559","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=7559"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7559\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}