求知 文章 文库 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查询表数据
860 次浏览
8次  

在本教程中,您将学习如何从node.js应用程序中查询MySQL中的表中的数据。

从node.js应用程序查询MySQL数据库中的数据的步骤如下:

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

执行SELECT语句并处理结果集。

关闭数据库连接。

执行一个简单的查询

以下select.js程序从todoapp数据库的todos表中选择所有数据:

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

let connection = mysql.createConnection(config);

let sql = `SELECT * FROM todos`;
connection.query(sql, (error, results, fields) => {
if (error) {
return console.error(error.message);
}
console.log(results);
});

connection.end();

执行上面代码,得到以下结果 -

F:\worksp\mysql\nodejs\nodejs-connect>node select.js
openssl config failed: error:02001003:system library:fopen:No such process
[ RowDataPacket { id: 1, title: 'Learn how to insert a new row', completed: 1 },
RowDataPacket {
id: 2,
title: 'Insert a new row with placeholders',
completed: 0 },
RowDataPacket { id: 3, title: 'Insert multiple rows at a time', completed: 0 },
RowDataPacket { id: 4, title: 'It should work perfectly', completed: 1 },
RowDataPacket { id: 5, title: 'Insert multiple rows at a time', completed: 0 },
RowDataPacket { id: 6, title: '现在学习一次插入多行记录(by www.yiibai.com)', completed: 1 },
RowDataPacket { id: 7, title: 'It should work perfectly', completed: 1 } ]

它按预期返回7行。

#将数据传递给查询

以下select2.js程序仅选择完成的工作:

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

let connection = mysql.createConnection(config);

let sql = `SELECT * FROM todos WHERE completed=?`;
connection.query(sql, [true], (error, results, fields) => {
if (error) {
return console.error(error.message);
}
console.log(results);
});

connection.end();

执行上面代码,得到以下结果 -

F:\worksp\mysql\nodejs\nodejs-connect>node select2.js
openssl config failed: error:02001003:system library:fopen:No such process
[ RowDataPacket { id: 1, title: 'Learn how to insert a new row', completed: 1 },
RowDataPacket { id: 4, title: 'It should work perfectly', completed: 1 },
RowDataPacket { id: 6, title: '现在学习一次插入多行记录(by www.yiibai.com)', completed: 1 },
RowDataPacket { id: 7, title: 'It should work perfectly', completed: 1 } ]

在这个例子中,我们使用问号(?)作为completed字段的占位符值。

当我们调用query()方法时,传递一个数组作为第二个参数的值,占位符将被序列的值替换。

select2.js程序返回两行,其中completed列为1,这意味着在Node.js中为true。

防止SQL注入

假设你想根据命令行中的参数的id来查询一个todo,可以使用以下代码(select3.js):

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

let connection = mysql.createConnection(config);

let id = process.argv[2]; // pass argument to query
let sql = `SELECT * FROM todos WHERE id=` + id ;

connection.query(sql, (error, results, fields) => {
if (error) {
return console.error(error.message);
}
console.log(results);
});

connection.end();

执行上面示例代码,得到以下结果 -

F:\worksp\mysql\nodejs\nodejs-connect>node select3.js 1
openssl config failed: error:02001003:system library:fopen:No such process
[ RowDataPacket { id: 1, title: 'Learn how to insert a new row', completed: 1 } ]

它正常工作。但是,有一个问题是可疑用户可能通过传递参数中的SQL代码来利用该程序。

为了防止这种SQL注入,您需要使用前一个示例中的占位符(?),或者使用mysql或连接对象的escape()方法,如下所示:

let sql = `SELECT * FROM todos WHERE id = ` + mysql.escape(id);

在本教程中,您已经学习了如何从node.js程序查询MySQL数据库中的数据。


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

1元 10元 50元





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



860 次浏览
8次
 捐助