Node学习(七)01-mysql基本用法——Node中使用MySQL模块的5个步骤 & select查询语句-查询的结果是一个数组,数组中的每个对象就是一行数据 & SQL中有多个占位符
9.2 mysql基本用法
在Node中使用MySQL模块一共需要5个步骤:
-
加载 MySQL 模块
-
创建 MySQL 连接对象
-
连接 MySQL 服务器
-
执行SQL语句
-
关闭链接
// 1. 加载mysql模块
const mysql = require('mysql');
// 2. 创建连接对象
const conn = mysql.createConnection({
// 键: 值
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'yingxiong'
});
// 3. 连接到MySQL服务器
conn.connect(); //调用connect方法。连接到MySQL服务器
// 4. 查询(增删改查)
/*
conn.query(SQL语句, [SQL中的占位符的值], (err, result, fields) => {
err: 错误信息
result: 查询的结果
fields: 查询的字段信息,一般来说不用
});
*/
conn.query('select * from heroes where id<4', (err, result, fields) => {
if (err) throw err;
console.log(result);
});
// 5. 关闭连接,释放资源
conn.end();
9.3 查询 — read
执行查询类型的SQL语句,查询结果(result)是一个数组,每个单元是对象,对象的属性是数据表的字段名
- 基本的查询
// 1. 加载mysql模块
const mysql = require('mysql');
// 2. 创建连接对象(顺便设置连接参数)
const conn = mysql.createConnection({
// 属性:值
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'yingxiong'
});
// 3. 连接到mysql服务器
conn.connect();
// 4. 查询
// select语句: 查询的结果是一个数组,数组中的每个对象就是一行数据
let sql = 'select * from heroes limit 1';
conn.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
});
// 5. 关闭连接
conn.end();
- 占位符模式
当SQL语句中使用了占位符,则query方法需要使用参数2
// 1. 加载mysql模块
const mysql = require('mysql');
// 2. 创建连接对象(顺便设置连接参数)
const conn = mysql.createConnection({
// 属性:值
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'yingxiong'
});
// 3. 连接到mysql服务器
conn.connect();
// 4. 查询
// select语句: 查询的结果是一个数组,数组中的每个对象就是一行数据
let sql = 'select * from heroes where id < ?';
conn.query(sql, 3, (err, result) => {
if (err) throw err;
console.log(result);
});
// 5. 关闭连接
conn.end();
如果SQL中有多个占位符,则传递数组
// 1. 加载mysql模块
const mysql = require('mysql');
// 2. 创建连接对象(顺便设置连接参数)
const conn = mysql.createConnection({
// 属性:值
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'yingxiong'
});
// 3. 连接到mysql服务器
conn.connect();
// 4. 查询
// select语句: 查询的结果是一个数组,数组中的每个对象就是一行数据
/// SQL中有多个占位符
let sql = 'select * from heroes where id < ? and sex = ?';
conn.query(sql, [3, '女'], (err, result) => {
if (err) throw err;
console.log(result);
});
// 5. 关闭连接
conn.end();
可以一次性执行多条SQL:
-
一次性执行多条SQL,多条SQL之间用 分号(;) 隔开
-
需要在连接参数哪里进行配置,加入
- 得到的结果是二维数组
// 整体的结果
[
// 下面是第一条SQL的查询结果
[
{
},
{
},
],
// 下面是第二条SQL查询的结果
[
{
},
{
},
],
// 下面是第三条SQL查询的结果
[
{
},
{
},
]
]
代码:
/// 执行多条SQL
let sql = `select id,name from heroes where id<3;
select * from boy;
select * from girl`;
conn.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
});
查询结果:
[
[
{
id: 1, name: '薇恩' },
{
id: 2, name: '赵信' }
],
[
{
name: '犀利哥', flower: '百合' },
{
name: '周杰伦', flower: '桃花' },
{
name: '小沈阳', flower: '梅花' },
{
name: '张三丰', flower: '荷花' },
{
name: '刘德华', flower: '狗尾巴花' }
],
[
{
name: '凤姐', flower: '仙人掌' },
{
name: '林志玲', flower: '荷花' },
{
name: '大S', flower: '梅花' },
{
name: '西施', flower: '桃花' },
{
name: '芙蓉姐姐', flower: '百合' }
]
]
实例
练习MySQL的代码\01-查询.js
// 1. 加载mysql模块
const mysql = require('mysql');
// 2. 创建连接对象(顺便设置连接参数)
const conn = mysql.createConnection({
// 属性:值
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'yingxiong',
multipleStatements: true // 表示可以一次性执行多条SQL
});
// 3. 连接到mysql服务器
conn.connect();
// 4. 查询
// select语句: 查询的结果是一个数组,数组中的每个对象就是一行数据
/// 基本查询
/*
let sql = 'select * from heroes limit 1';
conn.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
});
*/
/// SQL中有占位符
// let sql = 'select * from heroes where id < ?';
// conn.query(sql, 3, (err, result) => {
// if (err) throw err;
// console.log(result);
// });
/// SQL中有多个占位符
// let sql = 'select * from heroes where id < ? and sex = ?';
// conn.query(sql, [3, '女'], (err, result) => {
// if (err) throw err;
// console.log(result);
// });
/// 执行多条SQL
let sql = `select id,name from heroes where id<3;
select * from boy;
select * from girl`;
conn.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
});
// 5. 关闭连接
conn.end();
node中执行命令
node 相对路径
效果图-查询命令