{"id":161117,"date":"2024-11-04T03:45:28","date_gmt":"2024-11-03T19:45:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/161117\/"},"modified":"2024-11-04T03:45:29","modified_gmt":"2024-11-03T19:45:29","slug":"%e7%8d%b2%e5%be%97%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84%e7%9a%84%e6%96%b9%e6%b3%95%e7%b0%a1%e4%bb%8b-%e6%80%8e%e9%ba%bc%e7%8d%b2%e5%be%97%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/161117\/","title":{"rendered":"\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\u7c21\u4ecb (\u600e\u9ebc\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44)"},"content":{"rendered":"<h1 id=\"%e7%8d%b2%e5%be%97%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84%e7%9a%84%e6%96%b9%e6%b3%95%e7%b0%a1%e4%bb%8b-%e6%80%8e%e9%ba%bc%e7%8d%b2%e5%be%97%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84-YMYNRJfGgN\">\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\u7c21\u4ecb (\u600e\u9ebc\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44)<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u6578\u7d44\u7684\u7ba1\u7406\u548c\u7372\u53d6\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u6578\u64da\u5eab\u6578\u7d44\uff08Database Array\uff09\u662f\u4e00\u7a2e\u5c07\u6578\u64da\u4ee5\u6578\u7d44\u5f62\u5f0f\u5b58\u5132\u7684\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u64cd\u4f5c\u548c\u67e5\u8a62\u66f4\u52a0\u9ad8\u6548\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u9019\u4e00\u6280\u8853\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8sql%e6%9f%a5%e8%a9%a2%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-YMYNRJfGgN\">1. \u4f7f\u7528SQL\u67e5\u8a62\u7372\u53d6\u6578\u64da<\/h2>\n<p>\u6700\u5e38\u898b\u7684\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\u662f\u901a\u904eSQL\u67e5\u8a62\u3002SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u7528\u65bc\u7ba1\u7406\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u6a19\u6e96\u8a9e\u8a00\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528SQL\u67e5\u8a62\u4f86\u7372\u53d6\u6578\u64da\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u6578\u7d44\uff1a<\/p>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u5f9e\u540d\u70ba\u201cusers\u201d\u7684\u8868\u4e2d\u9078\u64c7\u6240\u6709\u8a18\u9304\u3002\u63a5\u4e0b\u4f86\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u7de8\u7a0b\u8a9e\u8a00\uff08\u5982PHP\u3001Python\u7b49\uff09\u4f86\u5c07\u67e5\u8a62\u7d50\u679c\u8f49\u63db\u70ba\u6578\u7d44\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8php%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84-YMYNRJfGgN\">2. \u4f7f\u7528PHP\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PDO\uff08PHP Data Objects\uff09\u6216MySQLi\u4f86\u57f7\u884cSQL\u67e5\u8a62\u4e26\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$dsn = 'mysql:host=localhost;dbname=testdb';\n$username = 'root';\n$password = '';\n\ntry {\n    $pdo = new PDO($dsn, $username, $password);\n    $stmt = $pdo-&gt;query('SELECT * FROM users');\n    $usersArray = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);\n} catch (PDOException $e) {\n    echo 'Connection failed: ' . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e86\u4e00\u500bPDO\u9023\u63a5\uff0c\u7136\u5f8c\u57f7\u884c\u67e5\u8a62\uff0c\u6700\u5f8c\u4f7f\u7528fetchAll\u65b9\u6cd5\u5c07\u7d50\u679c\u8f49\u63db\u70ba\u95dc\u806f\u6578\u7d44\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8python%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84-YMYNRJfGgN\">3. \u4f7f\u7528Python\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SQLite\u6216MySQL Connector\u7b49\u5eab\u4f86\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL Connector\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\nconnection = mysql.connector.connect(\n    host='localhost',\n    user='root',\n    password='',\n    database='testdb'\n)\n\ncursor = connection.cursor()\ncursor.execute('SELECT * FROM users')\nusers_array = cursor.fetchall()\n\nfor user in users_array:\n    print(user)\n\ncursor.close()\nconnection.close()<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u9996\u5148\u5efa\u7acb\u4e86\u4e00\u500b\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u7136\u5f8c\u57f7\u884c\u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5728\u4e00\u500b\u6578\u7d44\u4e2d\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8javascript%e5%92%8cnode-js%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e7%b5%84-YMYNRJfGgN\">4. \u4f7f\u7528JavaScript\u548cNode.js\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44<\/h2>\n<p>\u5728Node.js\u74b0\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528mysql\u6216pg\u7b49\u5eab\u4f86\u7372\u53d6\u6578\u64da\u5eab\u6578\u7d44\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528mysql\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const mysql = require('mysql');\n\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: '',\n    database: 'testdb'\n});\n\nconnection.connect();\n\nconnection.query('SELECT * FROM users', (error, results) =&gt; {\n    if (error) throw error;\n    console.log(results);\n});\n\nconnection.end();<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5728Node.js\u4e2d\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\uff0c\u6700\u7d42\u5c07\u7d50\u679c\u4ee5\u6578\u7d44\u5f62\u5f0f\u8f38\u51fa\u3002<\/p>\n<h2 id=\"5-%e7%b8%bd%e7%b5%90-YMYNRJfGgN\">5. \u7e3d\u7d50<\/h2>\n<p>\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\u591a\u7a2e\u591a\u6a23\uff0c\u7121\u8ad6\u662f\u4f7f\u7528SQL\u67e5\u8a62\uff0c\u9084\u662f\u901a\u904e\u4e0d\u540c\u7684\u7de8\u7a0b\u8a9e\u8a00\u4f86\u5be6\u73fe\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u81ea\u5df1\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\u3002\u638c\u63e1\u9019\u4e9b\u6280\u8853\u4e0d\u50c5\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u9084\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u7372\u5f97\u6578\u64da\u5eab\u6578\u7d44\u7684\u65b9\u6cd5\uff0c\u638c\u63e1\u6709\u6548\u7684\u6578\u64da\u63d0\u53d6\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u8207\u5206\u6790\u80fd\u529b\u3002<\/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-161117","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161117","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=161117"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161117\/revisions"}],"predecessor-version":[{"id":161118,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161117\/revisions\/161118"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=161117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=161117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=161117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}