const mysql = require('../libs/mysql');
const password = require('../libs/password');
(async ()=>{
let db = await mysql;
let name = 'blue';
let pass = '123456';
// 检查
let rows = await db.query("SELECT * FROM admin_table WHERE username=?", [name]);
if(rows.length>0) {
console.log(`管理员账号已存在:${name}`);
}else{
await db.query("INSERT INTO admin_table (username,password) VALUES(?,?)", [name, password(pass)]);
console.log('管理员添加成功');
}
})();
好玩的东西
const readline = require('readline');
let rl = readline.createInterface({
input: process.stdin, // 标准输入流
output: process.stdout, // 标准输出流
});
rl.question('what is your name ', name=>{
console.log('hi:', name)
})
add_admin.js:
const mysql = require('../libs/mysql');
const password = require('../libs/password');
const rl = require('../libs/rl');
(async ()=>{
let db = await mysql;
while(1) {
let name = await rl.questionAsync('admin username: ');
if(!name){
break;
}
// 检查
let rows = await db.query("SELECT * FROM admin_table WHERE username=?", [name]);
if(rows.length>0) {
console.log(`管理员账号已存在:${name}`);
}else{
let pass = await rl.questionAsync('admin password: ');
await db.query("INSERT INTO admin_table (username,password) VALUES(?,?)", [name, password(pass)]);
console.log('管理员添加成功');
}
}
rl.close();
})();