Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Node.js MySQL 插入数据


插入数据到表中

要填充 MySQL 中的表,请使用 "INSERT INTO" 语句。

示例

在 "customers" 表中插入一条记录

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("连接成功!");
  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 条记录");
  });
});
运行示例 »

将上面的代码保存到名为 "demo_db_insert.js" 的文件中,然后运行该文件

运行 "demo_db_insert.js"

C:\Users\Your Name>node demo_db_insert.js

您将得到以下结果

连接成功!
插入 1 条记录


插入多条记录

要插入多条记录,请创建一个包含值的数组,并在 sql 中插入一个问号,该问号将被值数组替换
INSERT INTO customers (name, address) VALUES ?

示例

填充 "customers" 表的数据

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("连接成功!");
  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("插入的记录数: " + result.affectedRows);
  });
});
运行示例 »

将上面的代码保存到名为 "demo_db_insert_multple.js" 的文件中,然后运行该文件

运行 "demo_db_insert_multiple.js"

C:\Users\Your Name>node demo_db_insert_multiple.js

您将得到以下结果

连接成功!
插入的记录数: 14

结果对象

执行查询时,会返回一个结果对象。

结果对象包含有关查询如何影响表的信息。

上面示例中返回的结果对象如下所示

{
  fieldCount: 0,
  affectedRows: 14,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '\'Records:14  Duplicated: 0  Warnings: 0',
  protocol41: true,
  changedRows: 0
}

可以像这样显示属性的值

示例

返回受影响的行数

console.log(result.affectedRows)

这将产生以下结果

14

获取插入的 ID

对于具有自动递增 ID 字段的表,您可以通过询问结果对象来获取刚刚插入行的 ID。

注意: 要获取插入的 ID,只能插入一行

示例

在 "customers" 表中插入一条记录,并返回 ID

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 条记录,ID: " + result.insertId);
  });
});
运行示例 »

将上面的代码保存到名为 "demo_db_insert_id.js" 的文件中,然后运行该文件

运行 "demo_db_insert_id.js"

C:\Users\Your Name>node demo_db_insert_id.js

这将返回类似于以下内容的结果

插入 1 条记录,ID: 15

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.