{"id":674,"date":"2024-07-21T13:11:39","date_gmt":"2024-07-21T13:11:39","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=674"},"modified":"2024-07-21T13:25:17","modified_gmt":"2024-07-21T13:25:17","slug":"nodejs-mysql-promise","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/07\/21\/nodejs-mysql-promise\/","title":{"rendered":"[Nodejs]mysql+promise"},"content":{"rendered":"\n<p>mysql+promise \uc0ac\uc6a9\uc5d0 \ub300\ud55c \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>\uad75\uc740\uae00\uc528 \ubd80\ubd84\uc774  mysql\uacfc promise\ub97c \uc0ac\uc6a9\ud558\ub294\ubd80\ubd84\uc785\ub2c8\ub2e4.<br>\uc544\ub798\uc640 \uac19\uc774 \uc0ac\uc6a9\ud558\uc2dc\uba74 mysql\uc791\uc5c5\uc744 \uc21c\uc11c\ub300\ub85c \ucc98\ub9ac \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>rows\ubcc0\uc218 \ubd80\ubd84\uc774 \uc81c\uc77c \ucc98\uc74c \uc791\uc5c5\uc774 \ub05d\ub098\uba74 rows2\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \uc2dc\uc791\ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<p>This is code for using mysql+promise.<br>The part in bold is the part that uses mysql and promise.<br>When the rows variable part is finished first, the rows2 part begins.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>const rows = await promisePrint(hostname, dbuser, dbpassword, dbname, \"username\");\nconst rows2 = await promisePrint(hostname, dbuser, dbpassword, dbname, \"username2\");<\/strong><\/code><\/pre>\n\n\n\n<p>\uc804\uccb4\ucf54\ub4dc(full code)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const mysql      = require('mysql2');\nconst express = require('express');\n<strong>const dbname = \"your_db_name\";\nconst hostname = \"localhost\";\nconst dbuser = \"your_db_user_name\";\nconst dbpassword = \"your_db_password\";<\/strong>\nconst { createServer } = require('http');\n\n\/\/ Initialize Express app and HTTP server\nconst app = express();\nconst server = createServer(app);\n\n<strong>async function  promisePrint(hostname, dbuser, dbpassword, dbname, username) {\n    const connection = mysql.createConnection({\n        host: hostname,\n        user: dbuser,\n        password: dbpassword,\n        database: dbname\n    });\n\n    return new Promise((resolve, reject) => {\n        var sql = 'SELECT * FROM video WHERE username = ? ORDER BY id DESC';\n        var values = &#91;username];\n        connection.query(sql, values, (error, rows) => {\n            if (error) {\n                reject(error);\n            } else {\n                resolve(rows);\n            }\n        });\n    }).finally(() => {\n        connection.end((err) => {\n            if (err) {\n                console.error('Error closing the connection:', err);\n            } else {\n                console.log(` ${username} Connection closed successfully.`);\n            }\n        });\n    });\n}<\/strong>\n\n\napp.get(\"\/\", async(req, res) => {\n\n    const connection = mysql.createConnection({\n        host     : hostname,\n        user     : dbuser,\n        password : dbpassword,\n        database : dbname\n      });\n  \n<strong>      try {\n          const rows = await promisePrint(hostname, dbuser, dbpassword, dbname, \"username\");\n          console.log('username:',rows);\n\n      } catch (error) {\n          console.error('Error fetching data:', error);\n          res.status(500).send('Internal Server Error');\n      }<\/strong>\n\n  });\n\n  \/\/ Start the server on port 3000 or the port specified in the environment variables\n  const port = process.env.PORT || 3000;\n  server.listen(port, () => {\n    console.log(`Server running at http:\/\/localhost:${port}`);\n  });\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>mysql+promise \uc0ac\uc6a9\uc5d0 \ub300\ud55c \ucf54\ub4dc \uc785\ub2c8\ub2e4.\uad75\uc740\uae00\uc528 \ubd80\ubd84\uc774 mysql\uacfc promise\ub97c \uc0ac\uc6a9\ud558\ub294\ubd80\ubd84\uc785\ub2c8\ub2e4.\uc544\ub798\uc640 \uac19\uc774 \uc0ac\uc6a9\ud558\uc2dc\uba74 mysql\uc791\uc5c5\uc744 \uc21c\uc11c\ub300\ub85c \ucc98\ub9ac \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.rows\ubcc0\uc218 \ubd80\ubd84\uc774 \uc81c\uc77c \ucc98\uc74c \uc791\uc5c5\uc774 \ub05d\ub098\uba74 rows2\ubd80\ubd84\uc758 \uc791\uc5c5\uc744 \uc2dc\uc791\ud569\ub2c8\ub2e4. This is code for using mysql+promise.The part in bold is the part that uses mysql and promise.When the rows variable part is finished first, the rows2 part begins. \uc804\uccb4\ucf54\ub4dc(full code)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-nodejs","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":5,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":679,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions\/679"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}