با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن ، با یکی دیگر از جلسات دوره کامل آموزش Node.js در خدمت شما دوستان هستیم. در این جلسه قصد داریم به آموزش درج اطلاعات در MySQL با Node.js بپردازیم.
از دستور INSERT INTO برای وارد کردن اطلاعات به یک جدول در MySQL استفاده می کنیم.
در مثال زیر یک رکورد را در جدول customers درج می کنیم:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')"; con.query(sql, function (err, result) { if (err) throw err; console.log("1 record inserted"); }); }); |
کد بالا را در فایلی به نام demo_db_insert.js ذخیره و اجرا کنید:
C:\Users\Your Name>node demo_db_insert.js |
خروجی:
Connected! ۱ record inserted |
برای درج چند رکورد، یک آرایه شامل مقادیر ایجاد کنید و یک علامت سوال ؟ در کوئری قرار دهید که آرایه مقادیر جابهجا خواهند شد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); var sql = "INSERT INTO customers (name, address) VALUES ?"; var values = [ ['John', 'Highway 71'], ['Peter', 'Lowstreet 4'], ['Amy', 'Apple st 652'], ['Hannah', 'Mountain 21'], ['Michael', 'Valley 345'], ['Sandy', 'Ocean blvd 2'], ['Betty', 'Green Grass 1'], ['Richard', 'Sky st 331'], ['Susan', 'One way 98'], ['Vicky', 'Yellow Garden 2'], ['Ben', 'Park Lane 38'], ['William', 'Central st 954'], ['Chuck', 'Main Road 989'], ['Viola', 'Sideway 1633'] ]; con.query(sql, [values], function (err, result) { if (err) throw err; console.log("Number of records inserted: " + result.affectedRows); }); }); |
کد بالا را در فایلی به نام demo_db_insert_multiple.js ذخیره و اجرا کنید:
C:\Users\Your Name>node demo_db_insert_multiple.js |
خروجی:
Connected! Number of records inserted: 14 |
هنگام اجرا یک کوئری، یک شی result برگشت داده خواهد شد.
این شی حاوی اطلاعاتی درمورد چگونگی تاثیر کوئری بر جدول است.
شی result برگشت داده شده از مثال بالا به صورت زیر می باشد:
1 2 3 4 5 6 7 8 9 10 | { fieldCount: 0, affectedRows: 14, insertId: 0, serverStatus: 2, warningCount: 0, message: '\'Records:14 Duplicated: 0 Warnings: 0', protocol41: true, changedRows: 0 } |
مقادیر این شی توسط کد زیر می توانند نمایش داده شوند:
1 | console.log(result.affectedRows) |
خروجی:
۱۴ |
شما می توانید id رکورد های جدولی که یک فیلد id افزایشی دارد را از طریق شی result بدست آوردید.
نکته: برای بدست اوردن id ، فقط یک ردیف یا رکورد می تواند درج شود.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; var sql = "INSERT INTO customers (name, address) VALUES ('Michelle', 'Blue Village 1')"; con.query(sql, function (err, result) { if (err) throw err; console.log("1 record inserted, ID: " + result.insertId); }); }); |
کد بالا را در فایلی به نام demo_db_insert_id.js ذخیره و اجرا کنید:
C:\Users\Your Name>node demo_db_insert_id.js |
خروجی:
۱ record inserted, ID: 15 |
جلسه آموزش درج اطلاعات در MySQL با Node.js نیز به پایان رسید.
در جلسه بعد به اموزش دریافت اطلاعات از MySQL با Node.js خواهیم پرداخت. با ما همراه باشید.