数据库和json文件对比
传统数据库操作。增,删,改,查
查操作
代码如下
//导包
var mysql = require("mysql");
//创建一个和数据库的连接
var connection = mysql.createConnection({
host: "localhost", //数据库服务器的地址
user: "root", //账号
password: "root", //密码
database: "alicezat2", //数据库名
});
//打开连接
connection.connect();
//执行sql语句
connection.query(" select * from alicezation", (error, results, fields) => {
//错误对象,如果没有错误就返回null
//console. log(error);
//执行sq1语句得到的结果集.有错的话就是undefined.
console.log(results);
// 也可以通过下标取值
console.log(results[3].username);
//拿到的是字段的信息
console.log(fields);
});
//关闭连接
connection.end();
增操作
//导包
var mysql = require("mysql");
//创建一个和数据库的连接
var connection = mysql.createConnection({
host: "localhost", //数据库服务器的地址
user: "root", //账号
password: "root", //密码
database: "alicezat2", //数据库名
});
//打开连接
connection.connect();
let name="桐谷直叶";
let arms="单手剑·绿叶精魂"
//执行sql语句
connection.query(`insert into alicezation(name,arms) values ('${name}','${arms}');`, (error, results, fields) => {
if(error==null){
console.log(results);//返回一个对象
console.log(results.affectedRows)//返回的是受影响的行数,如果大于0新增成功
console.log(results.insertId)
}
});
//关闭连接
connection.end();
修操作
代码如下
//导包
var mysql = require("mysql");
//创建一个和数据库的连接
var connection = mysql.createConnection({
host: "localhost", //数据库服务器的地址
user: "root", //账号
password: "root", //密码
database: "alicezat2", //数据库名
});
//打开连接
connection.connect();
let id =4;
let name="朝田诗乃";
let arms="长弓·湮灭射线"
//执行sql语句
connection.query(`update alicezation set name='${name}',arms='${arms}' where id=${id};`, (error, results, fields) => {
if(error==null){
console.log(results);//返回一个对象
console.log(results.affectedRows)//返回的是受影响的行数,如果大于0新增成功
}
});
//关闭连接
connection.end();
删操作
代码如下
//导包
var mysql = require("mysql");
//创建一个和数据库的连接
var connection = mysql.createConnection({
host: "localhost", //数据库服务器的地址
user: "root", //账号
password: "root", //密码
database: "alicezat2", //数据库名
});
//打开连接
connection.connect();
let id=4;
//执行sql语句
connection.query(`delete from aliceztion where id=${id} `, (error, results, fields) => {
//错误对象,如果没有错误就返回null
//console. log(error);
//执行sq1语句得到的结果集.有错的话就是undefined.
if(error==null){
console.log(results);//返回一个对象
console.log(results.affectedRows)//返回的是受影响的行数,如果大于0新增成功
console.log(results.insertId)
}
});
//关闭连接
connection.end();
json文件操作
增操作
代码如下
//新增
addHero({ name, skill, icon }) {
let heros = getAllhero()
heros.push({
id: heros.length + 1,
name,
skill,
icon,
isDelete: false
})
//保存回去
if (!fs.writeFileSync(fileName, JSON.stringify(heros))) {
return true
} else {
return false
}
},
查操作
代码如下
//导入模块
const fs = require('fs')
const path = require('path')
//基地址
const fileName = path.join(__dirname, './data/hero.json')
function getAllhero() {
const heros = JSON.parse(fs.readFileSync(fileName, 'utf-8'))
return heros
}
module.exports = {
//获取所有数据
Herolist() {
const heros = JSON.parse(fs.readFileSync(fileName, 'utf-8'))
return heros
.filter(v => !v.isDelete)
.map(({ id, name, skill, icon }) => {
return {
id,
name,
skill,
icon
}
})
},
或根据ID查找
//根据id获取数据
Heroinfo(id) {
const heros = this.Herolist()
const filterArr = heros.filter(v => {
return v.id == id
})
if (filterArr[0]) {
return filterArr[0]
} else {
return false
}
},
删操作
代码如下
//根据id删除英雄(软删除)
deleHeroById(id) {
const heros = getAllhero()
const filterArr = heros.filter(v => {
return v.id == id
})
// console. log (filterArr) ;
if (filterArr[0]) {
filterArr[0].isDelete = true
//保存回去
if (!fs.writeFileSync(fileName, JSON.stringify(heros))) {
return true
}
else {
return false
}
} else {
return false
}
},
改操作
updaHero({ id, name, skill, icon }) {
let heros = getAllhero()
const filterArr = heros.filter(v => {
return v.id == id
})
// console.1og (filterArr) ;
if (filterArr[0]) {
filterArr[0].name = name
filterArr[0].skill = skill
filterArr[0].icon = icon
//保存回去
if (!fs.writeFileSync(fileName, JSON.stringify(heros))) {
return true
} else {
return false
}
} else {
return false
}
}
效果
hero.json文件
[
{
"id": 1,
"name": "Ugo",
"skill": "蔷薇之剑",
"icon": "http://127.0.0.1:7878/8a28e7b917123db9d3e8635dacc2827d",
"isDelete": false
},
{
"id": 2,
"name": "Krito",
"skill": "暗夜之剑",
"icon": "ebb1860895cdb8ca85b67ef81dbefe66",
"isDelete": false
},
{
"id": 3,
"name": "Alice",
"skill": "金桂之剑",
"icon": "http://127.0.0.1:7878/b41668cd9f9a17114b77c42057faa72f",
"isDelete": false
},
{
"id": 4,
"name": "Alobug",
"skill": "管理员",
"icon": "http://127.0.0.1:7878/7276939c89940f8f23b79aa7d0893d19",
"isDelete": false
}
]