当前位置:Gxlcms > 数据库问题 > 使用sequelize对数据库进行增删改查

使用sequelize对数据库进行增删改查

时间:2021-07-01 10:21:17 帮助过:22人阅读

由于本人对于命令比较执着,所以基本都是在命令下操作的,喜欢使用命令的可以使用Cmder,需要安装、配置的可以参考这篇文章:

https://www.cnblogs.com/ziyoublog/p/10416684.html

首先我们需要在自己的文件夹下运行一下cmd

  1. npm init -y

  

(-y)的主要目的是跳过配置一系列的package.json

其次我们需要安装两个sequelize和mysql2

  1. yarn add sequelize mysql2 -S
  2. 或者
  3. npm install sequelize mysql2 -S

  

接下来我们需要在根目录下新建一个js文件

  1. // index.js
  2. const Sequelize = require(‘sequelize‘)
  3. const sequelize = new Sequelize(
  4. ‘testseq‘, // 数据库名
  5. ‘root‘, // 用户名
  6. ‘root‘, // 密码
  7. {
  8. ‘dialect‘: ‘mysql‘, // 数据库使用mysql
  9. ‘host‘: ‘localhost‘, // 数据库服务器ip
  10. ‘port‘: 3306, // 数据库服务器端口
  11. ‘define‘: {
  12. ‘underscored‘: true
  13. }
  14. }
  15. )

  

上述操作是为了连接数据库的,可以通过以下代码验证:

  1. // 测试数据库是否连接成功
  2. sequelize
  3. .authenticate()
  4. .then(res => {
  5. console.log(‘Connection Success!‘)
  6. })
  7. .catch(err => {
  8. console.log(‘Connection Error‘)
  9. })

技术图片

证明连接成功!

建立一个模板:

  1. // 模板sequelize.define(‘表名‘, {}, {})
  2. const User = sequelize.define(
  3. ‘first‘, {
  4. id: {
  5. field: ‘id‘, // 字段名
  6. primaryKey: true,
  7. type: Sequelize.INTEGER, // 类型
  8. allowNull: false // 是否允许为空
  9. },
  10. name: {
  11. field: ‘name‘,
  12. primaryKey: true,
  13. type: Sequelize.STRING
  14. },
  15. password: {
  16. field: ‘password‘,
  17. primaryKey: true,
  18. type: Sequelize.STRING,
  19. allowNull: false
  20. }
  21. }, {
  22. tableName: ‘first‘,
  23. timestamps: false,
  24. freezeTableName: true
  25. }
  26. )

  

首先我们来实现往数据库添加数据:

  1. // 往数据库添加单条数据
  2. User.create({
  3. id: 1,
  4. name: ‘test1‘,
  5. password: ‘123456‘
  6. })

技术图片

 

 

 你就可以看到sql语句,接下来看看数据库有没有数据:

技术图片

证明插入成功

其次就是改操作:

  1. // 修改往数据库数据(通过id去修改name或者password)
  2. User.update({
  3. ‘name‘: ‘test2‘
  4. }, {
  5. ‘where‘: { ‘id‘: 1 }
  6. })

  

sql语句:

技术图片

 

 

 数据库:

技术图片

 

 name成功由test1变成了test2,证明成功!

查所有操作:

  1. // 查询所有
  2. User.findAll().then((res) => {
  3. console.log(res)
  4. })

  

技术图片

 

 查单个操作:

  1. // 查询单条
  2. User.findOne({
  3. ‘where‘: {
  4. ‘id‘: 1
  5. }
  6. }).then(res => {
  7. console.log(res)
  8. })

  

技术图片

 

 由于就只有一条数据,所以查出来的结果是一样的, 但是查询单个findOne、全部findAll。

接下来就是删除操作了:

  1. // 删除数据库中某条数据
  2. User.destroy({
  3. ‘where‘: {
  4. ‘id‘: 1
  5. }
  6. })

  

技术图片

 

 数据库:

技术图片

 

 已经顺利删除了。

以上操作需要在已经建立数据表的情况下。

完整代码:

  1. const Sequelize = require(‘sequelize‘)
  2. const sequelize = new Sequelize(
  3. ‘testseq‘, // 数据库名
  4. ‘root‘, // 用户名
  5. ‘root‘, // 密码
  6. {
  7. ‘dialect‘: ‘mysql‘, // 数据库使用mysql
  8. ‘host‘: ‘localhost‘, // 数据库服务器ip
  9. ‘port‘: 3306, // 数据库服务器端口
  10. ‘define‘: {
  11. ‘underscored‘: true
  12. }
  13. }
  14. )
  15. // 测试数据库是否连接成功
  16. // sequelize
  17. // .authenticate()
  18. // .then(res => {
  19. // console.log(‘Connection Success!‘)
  20. // })
  21. // .catch(err => {
  22. // console.log(‘Connection Error‘)
  23. // })
  24. // 模板sequelize.define(‘表名‘, {}, {})
  25. const User = sequelize.define(
  26. ‘first‘, {
  27. id: {
  28. field: ‘id‘,
  29. primaryKey: true,
  30. type: Sequelize.INTEGER,
  31. allowNull: false
  32. },
  33. name: {
  34. field: ‘name‘,
  35. primaryKey: true,
  36. type: Sequelize.STRING,
  37. allowNull: false
  38. },
  39. password: {
  40. field: ‘password‘,
  41. primaryKey: true,
  42. type: Sequelize.STRING,
  43. allowNull: false
  44. }
  45. }, {
  46. tableName: ‘first‘,
  47. timestamps: false,
  48. freezeTableName: true
  49. }
  50. )
  51. // 往数据库添加单条数据
  52. User.create({
  53. id: 1,
  54. name: ‘test1‘,
  55. password: ‘123456‘
  56. })
  57. // // 往数据库添加数据多条数据 遍历
  58. // const addData = [{
  59. // id: 5,
  60. // name: ‘yang5‘,
  61. // password: ‘123456‘
  62. // },
  63. // {
  64. // id: 6,
  65. // name: ‘yang6‘,
  66. // password: ‘123456‘
  67. // }
  68. // ]
  69. // for (let i = 0; i < addData.length; i++) {
  70. // User.create({
  71. // id: addData[i].id,
  72. // name: addData[i].name,
  73. // password: addData[i].password
  74. // })
  75. // }
  76. // 修改往数据库数据(通过id去修改name或者password)
  77. // User.update({
  78. // ‘name‘: ‘test2‘
  79. // }, {
  80. // ‘where‘: { ‘id‘: 1 }
  81. // })
  82. // 删除数据库中某条数据
  83. // User.destroy({
  84. // ‘where‘: {
  85. // ‘id‘: 1
  86. // }
  87. // })
  88. // 查询所有
  89. User.findAll().then((res) => {
  90. console.log(res)
  91. })
  92. // 查询单条
  93. User.findOne({
  94. ‘where‘: {
  95. ‘id‘: 1
  96. }
  97. }).then(res => {
  98. console.log(res)
  99. })

  

使用sequelize对数据库进行增删改查

标签:查询   use   端口   fir   文章   安装、配置   测试   ini   dial   

人气教程排行