使用sequelize对数据库进行增删改查
由于本人对于命令比较执着,所以基本都是在命令下操作的,喜欢使用命令的可以使用Cmder,需要安装、配置的可以参考这篇文章:
https://www.cnblogs.com/ziyoublog/p/10416684.html
首先我们需要在自己的文件夹下运行一下cmd
1 | npm init -y |
(-y)的主要目的是跳过配置一系列的package.json
其次我们需要安装两个sequelize和mysql2
1 2 3 | yarn add sequelize mysql2 -S 或者 npm install sequelize mysql2 -S |
接下来我们需要在根目录下新建一个js文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // index.js const Sequelize = require( 'sequelize' ) const sequelize = new Sequelize( 'testseq' , // 数据库名 'root' , // 用户名 'root' , // 密码 { 'dialect' : 'mysql' , // 数据库使用mysql 'host' : 'localhost' , // 数据库服务器ip 'port' : 3306, // 数据库服务器端口 'define' : { 'underscored' : true } } ) |
上述操作是为了连接数据库的,可以通过以下代码验证:
1 2 3 4 5 6 7 8 9 | // 测试数据库是否连接成功 sequelize .authenticate() .then(res => { console.log( 'Connection Success!' ) }) . catch (err => { console.log( 'Connection Error' ) }) |
证明连接成功!
建立一个模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // 模板sequelize.define('表名', {}, {}) const User = sequelize.define( 'first' , { id: { field: 'id' , // 字段名 primaryKey: true , type: Sequelize.INTEGER, // 类型 allowNull: false // 是否允许为空 }, name: { field: 'name' , primaryKey: true , type: Sequelize.STRING }, password: { field: 'password' , primaryKey: true , type: Sequelize.STRING, allowNull: false } }, { tableName: 'first' , timestamps: false , freezeTableName: true } ) |
首先我们来实现往数据库添加数据:
1 2 3 4 5 6 | // 往数据库添加单条数据 User.create({ id: 1, name: 'test1' , password: '123456' }) |
你就可以看到sql语句,接下来看看数据库有没有数据:
证明插入成功
其次就是改操作:
1 2 3 4 5 6 | // 修改往数据库数据(通过id去修改name或者password) User.update({ 'name' : 'test2' }, { 'where' : { 'id' : 1 } }) |
sql语句:
数据库:
name成功由test1变成了test2,证明成功!
查所有操作:
1 2 3 4 | // 查询所有 User.findAll().then((res) => { console.log(res) }) |
查单个操作:
1 2 3 4 5 6 7 8 | // 查询单条 User.findOne({ 'where' : { 'id' : 1 } }).then(res => { console.log(res) }) |
由于就只有一条数据,所以查出来的结果是一样的, 但是查询单个findOne、全部findAll。
接下来就是删除操作了:
1 2 3 4 5 6 | // 删除数据库中某条数据 User.destroy({ 'where' : { 'id' : 1 } }) |
数据库:
已经顺利删除了。
以上操作需要在已经建立数据表的情况下。
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | const Sequelize = require( 'sequelize' ) const sequelize = new Sequelize( 'testseq' , // 数据库名 'root' , // 用户名 'root' , // 密码 { 'dialect' : 'mysql' , // 数据库使用mysql 'host' : 'localhost' , // 数据库服务器ip 'port' : 3306, // 数据库服务器端口 'define' : { 'underscored' : true } } ) // 测试数据库是否连接成功 // sequelize // .authenticate() // .then(res => { // console.log('Connection Success!') // }) // .catch(err => { // console.log('Connection Error') // }) // 模板sequelize.define('表名', {}, {}) const User = sequelize.define( 'first' , { id: { field: 'id' , primaryKey: true , type: Sequelize.INTEGER, allowNull: false }, name: { field: 'name' , primaryKey: true , type: Sequelize.STRING, allowNull: false }, password: { field: 'password' , primaryKey: true , type: Sequelize.STRING, allowNull: false } }, { tableName: 'first' , timestamps: false , freezeTableName: true } ) // 往数据库添加单条数据 User.create({ id: 1, name: 'test1' , password: '123456' }) // // 往数据库添加数据多条数据 遍历 // const addData = [{ // id: 5, // name: 'yang5', // password: '123456' // }, // { // id: 6, // name: 'yang6', // password: '123456' // } // ] // for (let i = 0; i < addData.length; i++) { // User.create({ // id: addData[i].id, // name: addData[i].name, // password: addData[i].password // }) // } // 修改往数据库数据(通过id去修改name或者password) // User.update({ // 'name': 'test2' // }, { // 'where': { 'id': 1 } // }) // 删除数据库中某条数据 // User.destroy({ // 'where': { // 'id': 1 // } // }) // 查询所有 User.findAll().then((res) => { console.log(res) }) // 查询单条 User.findOne({ 'where' : { 'id' : 1 } }).then(res => { console.log(res) }) |
希望大佬看到有不对的地方,提出博主予以改正!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步