javascript连接数据库进行增删改查操作


常见数据库MySOL\Oracle\SQL Server\Mongodb
其中,MySOL\Oracle\SQL Server属于传统型数据库(关系型数据库)
Mongodb又称作非关系型数据库,在一定程度上弥补了传统型数据库的缺陷。 
 
传统数据库的组织结构:数据库、数据表、数据行、字段(相当于列 )
 
字段的特殊标识:
PK(primary Key)主键、唯一标识
NN(not Null)值不允许为空
UQ(unique)值唯一
A I(Auto Increment)值自动增长
 

SQL语句

-- 通过*把表中的数据查询出来
select * from users;
 
-- 从users表中把username和password对应的数据查询出来
select usrname,password from users;
 
-- 插入一条记录
-- insert into users (usrname,password) values('tony','123456');
 
-- 修改记录内容
-- update users set password='99999' where id=4;
 
-- 更新某一行的若干列
-- update users set password='987654321' ,status=1 where id=2;
 
-- 删除某行记录
-- delete from users where id=7;
 

WHERE子句

select * from users where status =1;
select * from users where id<>2;
select * from users where usrname ='ss’;
select * from users where id>3;
 

AND 和 OR运算符

select * from users where status=0 and id<4;
select * from users where status=1 or usrname='ss’;
 

Order by子句

-- 对users表中的数据,按照id字段进行降序排序-- 
select * from users order by id desc;
 默认升序asc
select * from users order by id ;

多重排序

-- 对users表中的数据,先按照status降序,再按照username升序
select * from users order by status desc,usrname asc;
 

COUNT(*)函数

统计数据条数
-- 使用count(*)统计users表中,状态为0的用户数量
select count(*) from users where status=0; 
 

用AS为列设置别名

select count(*) as total from users where status=0;
 

在项目中操作MySQL

首先建立数据库连接
//导入mysql模块
const mysql = require('mysql');
//建立数据库链接关系
const db = mysql.createPool({
    host: 'localhost',//数据库的ip地址
    user: 'root',//登陆数据库的账号
    password: '999813sch',//登陆数据库的密码
    database: 'my_newdb'//指定要操作那个数据库
});

进行测试,看数据库是否正常工作

// //测试模块是否正常工作
db.query('select 1', (err, results) => {
    if (err) {
        return console.log(err.messsge);
    }
    console.log(results);
})

查询功能

//1.查询users表中的所以数据
const sqlstr = "select * from users";
db.query(sqlstr, (err, results) => {
    if (err) {
        //查询失败
        return console.log(err.messsge);
    }
    //查询成功
    //如果是执行的select语句,则执行结果是数组
    console.log(results);
})
终端node运行js文件
查询结果
shuchenhao@shuchenhaodeMacBook-Air DB % node 1操作数据库.js
[
  RowDataPacket { id: 1, usrname: 'zs', password: '123456', status: 0 },
  RowDataPacket { id: 2, usrname: 'ss', password: '111', status: 0 },
  RowDataPacket { id: 3, usrname: 'www', password: '1', status: 0 }
]
//插入数据
// const user = { usrname: 'spiderman', password: 'spiderman' };
// //定义执行的sql语句
// const sqlstr1 = "insert into users (usrname,password) values (?,?)";
// //执行sql语句
// db.query(sqlstr1, [user.usrname, user.password], (err, results) => {
//     if (err) {
//         //插入失败
//         return console.log(err.messsge);
//     };
//     // if (results.affectedRows === 1) {
//     //     console.log('插入数据成功');
//     // }
//     console.log('插入数据成功');

// })

简洁方法

//插入数据
const user = [1, 'spiderman', '123', 1];
//定义执行的sql语句
const sqlstr1 = "insert into users  values(?,?,?,?);";
//执行sql语句
db.query(sqlstr1, user, (err, results) => {
    if (err) {
        //插入失败
        return console.log('插入失败' + err.messsge);
        results;
    };
    console.log('插入数据成功');
})
// //更新用户信息
const user1 = { id: 2, username: 'aaa', password: '0000' };
//定义sql语句
const sqlstr = "update users set username=?, password=? where id=?";
//执行sql语句
db.query(sqlstr, [user1.username, user1.password, user1.id], (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    // if (results.affectedRows == 1) {
    //     console.log('更新成功');
    // }
    console.log('更新成功');
})
//删除
const sqlstr = 'delete from users where id=1';
db.query(sqlstr, (err, results) => {
    if (err) {
        return console.log("删除失败" + err.message);
    }
    console.log('删除数据成功');
})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM