求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 

mysql教程
MySQL快速学习入门
MySQL是什么
MySQL安装
MySQL示例数据库
MySQL导入示例数据库
MySQL基础教程
MySQL查询数据
MySQL WHERE语句
MySQL插入数据
MySQL更新表数据
MySQL删除表数据
MySQL创建与删除数据库
MySQL创建表
MySQL修改表结构
MySQL重命名表
MySQL数据类型
高级部分
MySQL技巧
MySQL存储过程
MySQL视图
MySQL触发器
MySQL管理
MySQL全文搜索
MySQL函数
应用程序连接
MySQL+Node.js连接和操作
Python+MySQL连接和操作
 
 

Node.js连接MySQL插入表数据
912 次浏览
10次  

在本教程中,您将学习如何从node.js应用程序将一行或多行插入到表中。

要在表中插入新行,请按照下列步骤操作:

连接到MySQL数据库,参阅:
http://lib.uml.com.cn/ebook/mysql/mysql24-1.asp

通过在connection对象上调用query()方法来执行INSERT语句。

关闭数据库连接。

请注意,我们将重用包含MySQL数据库信息的config.js模块。

如果您没有从上一个教程开始,那么可以参考这里的config.js模块:

let config = {
host : 'localhost',
user : 'root',
password: '123456',
database: 'todoapp'
};

module.exports = config;

将一行数据插入表中

以下insert.js程序将向todos表中插入一个新行:

let mysql = require('mysql');
let config = require('./config.js');
let connection = mysql.createConnection(config);

// insert statment
let sql = `INSERT INTO todos(title,completed)
VALUES('Learn how to insert a new row',true)`;

// execute the insert statment
connection.query(sql);

connection.end();

下面,我们来执行insert.js程序。

F:\worksp\mysql\nodejs\nodejs-connect>node insert.js
openssl config failed: error:02001003:system library:fopen:No such process

并检查todos表中的数据:

mysql> select * from todos;
+----+-------------------------------+-----------+
| id | title | completed |
+----+-------------------------------+-----------+
| 1 | Learn how to insert a new row | 1 |
+----+-------------------------------+-----------+
1 row in set (0.00 sec)

在上面查询结果中,您可以看到,程序向todos表中插入一个新行。

插入一行并返回插入的ID

以下insert-lastid.js程序将一个新行插入到todos表中,并返回插入的id。

let mysql = require('mysql');
let config = require('./config.js');
let connection = mysql.createConnection(config);

let stmt = `INSERT INTO todos(title,completed)
VALUES(?,?)`;
let todo = ['Insert a new row with placeholders', false];

// execute the insert statment
connection.query(stmt, todo, (err, results, fields) => {
if (err) {
return console.error(err.message);
}
// get inserted id
console.log('Todo Id:' + results.insertId);
});

connection.end();

要将数据传递给SQL语句,请使用问号(?)作为占位符。

在这个例子中,我们分别使用两个问号(?,?)作为title和completed字段。

执行查询后,可以从results对象的insertId属性中获取插入的id,如下所示 -

F:\worksp\mysql\nodejs\nodejs-connect>node insert-lastid.js
openssl config failed: error:02001003:system library:fopen:No such process
Todo Id:2

一次插入多行

以下insert-more.js程序一次将多行插入到todos表中:

let mysql = require('mysql');
let config = require('./config.js');

let connection = mysql.createConnection(config);

// insert statment
let stmt = `INSERT INTO todos(title,completed) VALUES ? `;
let todos = [
['Insert multiple rows at a time', false],
['现在学习一次插入多行记录(by www.yiibai.com)', true],
['It should work perfectly', true]
];

// execute the insert statment
connection.query(stmt, [todos], (err, results, fields) => {
if (err) {
return console.error(err.message);
}
// get inserted rows
console.log('Row inserted:' + results.affectedRows);
});

// close the database connection
connection.end();

请注意,我们在INSERT语句中只使用一个问号(?),多行数据是数组数组。

您可以访问通过results对象的affectedRows属性插入的行数。

F:\worksp\mysql\nodejs\nodejs-connect>node insert-more.js
openssl config failed: error:02001003:system library:fopen:No such process
Row inserted:3

如结果所示,插入了三行,这是像我们预期的那样。

在本教程中,您已经学习了如何从node.js程序将一行或多行插入到表中。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



912 次浏览
10次
 捐助