欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

js實現mysql的增刪查改

呂致盈1年前11瀏覽0評論

JavaScript是一種強大的腳本語言,可以與許多不同的數據庫進行交互。其中,MySQL作為最流行的關系型數據庫之一,在使用JavaScript的網站中被廣泛使用。在本文中,我們將介紹如何使用JavaScript實現MySQL的CRUD操作,即增加、刪除、查詢和修改數據。

1. 增加數據

var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase'
});
connection.connect(function(err) {
if (err) throw err;
console.log('Connected!');
var sql = "INSERT INTO mytable (name, age) VALUES ('John', 25)";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});

2. 刪除數據

var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase'
});
connection.connect(function(err) {
if (err) throw err;
console.log('Connected!');
var sql = "DELETE FROM mytable WHERE name = 'John'";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("Number of records deleted: " + result.affectedRows);
});
});

3. 查詢數據

var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase'
});
connection.connect(function(err) {
if (err) throw err;
console.log('Connected!');
var sql = "SELECT * FROM mytable";
connection.query(sql, function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});

4. 修改數據

var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase'
});
connection.connect(function(err) {
if (err) throw err;
console.log('Connected!');
var sql = "UPDATE mytable SET age = 30 WHERE name = 'John'";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
});
});

在本文中,我們演示了如何使用JavaScript實現MySQL的CRUD操作。當然,這只是冰山一角。要成為一個出色的開發人員,您需要深入學習這些構建塊,通過不斷地實踐和積累經驗,不斷提高自己的技能水平。祝您在編程路上取得成功!