美文网首页
Node-MySQL

Node-MySQL

作者: zhouhao_180 | 来源:发表于2019-06-11 10:29 被阅读0次

一、配置

这里使用phpStudy启动MySql服务,Navicat for MySQL具体操作数据库。

说明: npm 官网 搜索 mysql
安装: mysql模块 npm i mysql -S

二、使用

2.1、连接数据库

// 导入mysql模块
const mysql = require("mysql");
// 连接数据库
const conn = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "mysql001"
});

2.2、查

// 创建sql语句
const sqlStr1 = "select * from users";
// 执行sql语句
conn.query(sqlStr1, (err, result) => {
    if (err) return console.log("查询失败"+err.message);
    console.log(result);
});

2.3、增

const user = { uname: "zs", age: 12, gender: "男" };
// 创建sql语句 ? 为占位符
const sqlStr2 = "insert into users set ?";
// 执行sql语句
conn.query(sqlStr1, user, (err, result) => {
    if (err) return console.log("执行失败" + err.message);
    console.log(result);
});

2.4、改

const user = { id: 1, uname: "zs", age: 12, gender: "男" };
// 创建sql语句 ? 为占位符
const sqlStr3 = "update users set ? where id=?";
// 执行sql语句
conn.query(sqlStr3, [user, user.id], (err, result) => {
    if (err) return console.log("执行失败" + err.message);
    console.log(result);
});

2.5、删(建议伪删除)

const user = { id: 1 };
// 创建sql语句 ? 为占位符
const sqlStr4 = "delete from users where id=?";
// 执行sql语句
conn.query(sqlStr4, user.id, (err, result) => {
    if (err) return console.log("执行失败" + err.message);
    console.log(result);
});

相关文章

  • Nodejs学习笔记-Mysql连接池

    安装mysqlpool模块 npm install -g node-mysql 代码 https://github...

  • node-mysql

    1、bulk insert: 碰到自增的主键,需要列出插入的column,为了去除主键普通insert,碰到自增的...

  • Node-MySQL

    一、配置 这里使用phpStudy启动MySql服务,Navicat for MySQL具体操作数据库。 说明: ...

  • Node代码片段

    Node与Mysql交互 采用node-mysql开源项目作为Node框架的MySQL扩展库包括以下方面: 1. ...

  • node-mysql文档翻译

    模块Github地址 安装 如果需要以前的版本0.9.x系列的文档,请访问v0.9 branch.有时你可以从gi...

  • node-mysql(本地配置)

    本来想直接在公司的测试环境上练练 node 端mysql的api,但是一想自己也没装过 就自己装试试 ,事实证明 ...

  • node-mysql 连接池

    前言 具体代码发布在github了,不同分支代表在不同阶段的版本我的github createConnection...

  • node-Mysql数据库

    mysql数据库基础知识总结 一、基础常用命令 1.创建命令 create user ‘用户名’ @‘ip’ id...

  • 12-廖雪峰-node-mysql

    我们需要一个mysql 直接官网下载安装一路安装下去就是 安装完毕,我们要使用mysql自带的cmdps注意命令行...

  • 记 node-mysql 掉进 mysql 的 wait_tim

    公司 tms(Node.js + Python 开发的上线工具)碰到神奇的问题,『使用一段时间后』,express...

网友评论

      本文标题:Node-MySQL

      本文链接:https://www.haomeiwen.com/subject/jbcffctx.html