自封装node 的简单增删改查
1 首先引入的上篇node 链接mysql 里面的js
var connect = require('./nodemysql.js');
2 定义常量
const customerSQL = { queryAll: function(dbname, callback, obj) { //obj查找的条件 若没有全部返回 var where = '' if (obj == undefined) { where = '' } else { var s = ' WHERE ' for (var i in obj) { s += i + '="' + obj[i] + '" and ' } where = s.substring(0, s.lastIndexOf(' and')) } var sql = 'SELECT * FROM ' + dbname + where; console.log(sql) connect.query(sql, function(err, result) { if (err) { console.log('[SELECT ERROR] - ', err.message); callback({ code: 0, mes: err.message }) return; } callback({ code: 1, res: result }) return; }) // console.log(result); }, insert: function(dbname, callback, obj) { //obj插入的数据 var sql = 'insert into ' + dbname + ' set ?' console.log(sql) connect.query(sql, obj, function(err, result) { if (err) { console.log('[insert ERROR] - ', err.message); callback({ code: 0, mes: err.message }) return; } callback({ code: 1, res: result }) // { // "code": 1, // "res": { // "fieldCount": 0, // "affectedRows": 1, // "insertId": 16,//插入行的id // "serverStatus": 2, // "warningCount": 0, // "message": "", // "protocol41": true, // "changedRows": 0 // } // } return; }) }, update: function(dbname, callback, arr) { //[{},{}] 第一个obj是要改的数据 第二个obj是查询条件 var where = '' var obj = arr[1] if (obj == undefined) { callback({ code: 0, mes: 'no key' }); return; } else { var s = ' where ' for (var i in obj) { s += i + '="' + obj[i] + '" and ' } where = s.substring(0, s.lastIndexOf(' and')); } var sql = 'update ' + dbname + ' set ?' + where; connect.query(sql, arr[0], function(err, result) { if (err) { console.log('[update ERROR] - ', err.message); callback({ code: 0, mes: err.message }) return; } callback({ code: 1, res: result }); // { // "code": 1, // "res": { // "fieldCount": 0, // "affectedRows": 1, // "insertId": 0, // "serverStatus": 34, // "warningCount": 0, // "message": "(Rows matched: 1 Changed: 1 Warnings: 0", // "protocol41": true, // "changedRows": 1 //被改变的行数 // } // } return; }) }, delete: function(dbname, callback, obj) { var where = '' if (obj == undefined) { callback({ code: 0, mes: 'no key' }); return; } else { var s = ' WHERE ' for (var i in obj) { s += i + '="' + obj[i] + '" and ' } where = s.substring(0, s.lastIndexOf(' and')) } var sql = 'delete from ' + dbname + where; connect.query(sql, function(err, result) { if (err) { console.log('[delete ERROR] - ', err.message); callback({ code: 0, mes: err.message }) return; } callback({ code: 1, res: result }); // { // "code": 1, // "res": { // "fieldCount": 0, // "affectedRows": 1,//受影响的行数 // "insertId": 0, // "serverStatus": 2, // "warningCount": 0, // "message": "", // "protocol41": true, // "changedRows": 0 // } // } return; }) // console.log(result); }, };
3 、输出常量和connet
module.exports = { customerSQL, connect };
4、 使用
查询:
var { customerSQL, connect } = require('./customersql.js'); app.get('/bayid', function(req, res) { var i = req.query.id ? { id: req.query.id } : undefined var show = function(result) { res.send(result); } customerSQL.queryAll('fruit', show, i) });
修改:
router.post('/setname', function(req, res, next) { var ss = req.body.name if (ss == undefined) { res.json({ code: 0, message: '修改错误,没有key' }) return }; var nickname = { name: ss }; var show = function(result) { console.log(result) var r = {} if (result.code == 0) { res.json({ code: 0, message: '修改失败' }) } else { res.json({ code: 1, message: '修改成功' }) } }; customerSQL.update('user', show, [nickname, { id: 69 }]) });
自封装node 的简单增删改查的更多相关文章
- ado.net的简单数据库操作(三)——简单增删改查的实际应用
果然,在犯困的时候就该写写博客,写博客就不困了,哈哈! 上篇我记录了自己的SqlHelper的开发过程,今天记录一下如何使用这个sqlhelper书写一个具有简单增删改查的小实例啦. 实例描述:在数据 ...
- Redis:五种数据类型的简单增删改查
Redis简单增删改查例子 例一:字符串的增删改查 #增加一个key为ay_key的值 127.0.0.1:6379> set ay_key "ay" OK #查询ay_ke ...
- node 后台使用增删改查(4)
无论node还是java增删改查都是一样的原理,变得是配合框架使用时候有简便方法而已. 这里我接着上一篇开始讲,使用同一个数据库(数据库创建)这里必须创建了数据库 优化:为了维护方便这里我们把sql语 ...
- 国产化之路-统信UOS + Nginx + Asp.Net MVC + EF Core 3.1 + 达梦DM8实现简单增删改查操作
专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- 04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作
前面大概了解了一下elasticsearch的数据存储和数据的查询.现在学习一下,es的复杂操作. 官网相关文档地址:https://www.elastic.co/guide/en/e ...
- linq的简单增删改查
Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...
- Android_ADB 常用 shell命令 和 sqlite3 简单增删改查
今天学习了一个ADB的常用命令.接下来简单使用几个常用ADB shell 命令. 首先我们得明白什么是adb.exe ADB -Android Debug Bridge, 是 Android sdk ...
- MyBatis之二:简单增删改查
这一篇在上一篇的基础上简单讲解如何进行增删改查操作. 一.在mybatis的配置文件conf.xml中注册xml与注解映射 <!-- 注册映射文件 --> <mappers> ...
随机推荐
- MySQL [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause
MySQL[Err]1055 上次MySQL5.7.19主从建立完成之后,所有的测试都是在MySQL命令行下进行的,最近用Navicat Premium进行MySQL的连接,然后在插入数据的时候MyS ...
- 如何使用react-redux——傻瓜版
概述 之前看redux官方文档真是看得一脸懵逼,现在自认为会用了,于是来总结一下用法,供以后开发时参考,相信对其他人也有用. 不得不说,用了redux之后感觉挺爽的,有如下优点: 组件大多是函数组件非 ...
- react小知识
概述 有句话说得很好,代码是写给人看的,顺便让机器执行而已.所以我总结了一些写react不太注意的地方,供以后开发时参考,相信对其他人也有用. 组件封装 由于组件其实就是React.createEle ...
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
本题和上题一样同属于层次遍历,不同的是本题从底层往上遍历,如下: 代码如下: struct TreeNode { int val; TreeNode* left; TreeNode* right; T ...
- 记CTC原理
CTC,Connectionist temporal classification.从字面上理解它是用来解决时序类数据的分类问题.语音识别端到端解决方案中应用的技术.主要是解决以下两个问题 解决语音输 ...
- [EXP]Microsoft Windows CONTACT - Remote Code Execution
[+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org [+] Source: http://hyp3 ...
- Java 架构师+高并发+性能优化+Spring boot大型分布式项目实战
视频课程内容包含: 高级 Java 架构师包含:Spring boot.Spring cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat.Spring.MongoDB.Zer ...
- hdu 5116--Everlasting L(计数DP)
题目链接 Problem Description Matt loves letter L. A point set P is (a, b)-L if and only if there exists ...
- 课程四(Convolutional Neural Networks),第四 周(Special applications: Face recognition & Neural style transfer) —— 3.Programming assignments:Face Recognition for the Happy House
Face Recognition for the Happy House Welcome to the first assignment of week 4! Here you will build ...
- Ubuntu 16.04下安装谷歌浏览器(转)
1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令,将下载源加入到系统的源列表. sudo wget http://www ...