先安装npm模块项目

npm init

安装mysql

npm install mysql --save

Nodejs 连接msyql

// 导入mysql
const mysql = require('mysql'); // 连接mysql
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
port: '3306',
database: 'test'
}); connection.connect(); // 结束连接
connection.end();

// 引入mysql
const mysql = require('mysql'); // 连接myql
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
port: '3306',
database: 'test',
}); connection.connect(); // 插入语句
let addSql = "insert into article (title, author, date) values (?, ?, now())";
let addSqlParams = ['Today is noce', 'Bob']; // 执行插入语句
connection.query(addSql, addSqlParams, (err, result) => {
if (err) {
throw err;
} // 插入成功输出
console.log('插入成功');
console.log(result);
}); // 断开连接msyql
connection.end();

// 引入mysql
const mysql = require('mysql'); // 连接mysql
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
port: '3306',
database: 'test'
}); connection.connect(); // 删除语句
let sql = "delete from article where id = 10"; // 执行删除语句
connection.query(sql, (err, data) => {
if (err) {
throw err;
} // 执行成功
console.log('delete success!');
console.log(data);
}); // 断开连接msyql
connection.end();

// 导入mysql
const mysql = require('mysql'); // 连接mysql
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
port: '3306',
database: 'test'
}); connection.connect(); // 更新语句
let modSql = "update article set title = ?, author = ? where id like ?";
let modSqlParams = ['今晚学习nodejs', '一波万波', 12]; // 执行更新语句
connection.query(modSql, modSqlParams, (err, data) => {
if (err) {
throw err;
}
console.log('upload success!');
console.log(data)
}); connection.end();

// 导入mysql
const mysql = require('mysql'); // 连接mysql
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
port: '3306',
database: 'test',
}); connection.connect(); // 查询语句
let sql = 'SELECT * FROM article'; // 执行查询语句
connection.query(sql, (err, data) => {
if (err) {
console.log('[SELECT ERROR] - ', err.message);
return;
} // 查询成功
console.log(data);
});
connection.end();

Nodejs操作MySQL - 增删改查的更多相关文章

  1. nodejs+express+mysql 增删改查

    之前,一直使用的是nodejs+thinkjs来完成自己所需的项目需求,而对于nodejs中另外一中应用框架express却了解的少之又少,这两天就简单的了解了一下如何使用express来做一些数据库 ...

  2. 简单实现 nodejs koa2 mysql 增删改查 制作接口

    1.首先 在电脑上安装 nodejs (此处略过) 2.全局安装 koa2 (这里使用的淘宝镜像cnpm,有兴趣的同学可以自行搜索下) cnpm install koa-generator -g 3. ...

  3. nodejs+express+mysql 增删改查(二)

    1.最早一篇关于express框架简单的增删改查文章,http://www.cnblogs.com/zhengyeye/p/nodejs.html#3947308:意外走红博客园,无奈自己之前一直没有 ...

  4. 洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查

    首先你得学会基本的mysql操作语句:mysql学习 其次,python要想操作mysql,靠python的内置模块是不行的,而如果通过os模块调用cmd命令虽然原理上是可以的,但是还是不太方便,那么 ...

  5. nodejs利用express操作mysql增删改查

    如果不知道怎么连接数据库的请看http://www.cnblogs.com/complete94/p/6714757.html 我当大家都知道怎么连接数据库了,那么 我们开始吧 var express ...

  6. php部分--操作MySQL 增删改查

    1.建立主页 并进行信息的显示和查询 <table width="95%" border="1" cellpadding="0" ce ...

  7. Django学习之mysql增删改查

    上节介绍了如何使用命令行操作mysql增删改查,现在介绍如何使用python管理mysql 使用pip 下载完mysql后,mysql会以pymysql模块的形式存储在pycharm的包文件里.我们通 ...

  8. MySQL数据分析(16)— 数据操作之增删改查

    前面我们说学习MySQL要从三个层面,四大逻辑来学,三个层面就是库层面,表层面和数据层面对吧,数据库里放数据表,表里放数据是吧,大家可以回忆PPT中jacky的这图,我们已经学完了库层面和表层面,从本 ...

  9. mysql增删改查相关操作

    mysql增删改查相关操作 以前用mysql用的少,对于数据库相关的操作不熟悉,现在开始要接触数据库了,记录一下相关的基础操作吧. 1.数据库的授权操作 # mysql -u root -p Ente ...

随机推荐

  1. git commit 报错 There was a problem with the editor 'vi'

    今天提交代码git  commit -a的时候出现了这个错误 上网查了一下,说是Vim的返回值出错了,出错的原因还在翻译英文, 先贴出解决方案吧 git config --global core.ed ...

  2. jQuery中的Deferred和promise

    promise:http://www.alloyteam.com/2014/05/javascript-promise-mode/ 1 jQuery 中的 Deferred 和 Promises : ...

  3. 广义逆高斯分布(Generalized Inverse Gaussian Distribution)及修正贝塞尔函数

    1. PDF generalized inverse Gaussian distribution (GIG) 是一个三参数的连续型概率分布: f(x)=(a/b)p/22Kp(ab−−√)xp−1e− ...

  4. 10.24的注意事项——解决linux_jni编译错误的问题

    公司以opus开源库.因此,我们遇到了一些问题. 我将新下载的opus1.1替换掉老版本号之后,单独编译opus没问题.但是编译相关的文件就会报错. 错误信息例如以下: g++ -Wall -fPIC ...

  5. C#选择多个文件并读取多个文件数据

    原文:C#选择多个文件并读取多个文件数据 版权声明:本文为博主原创文章,转载请附上链接地址. https://blog.csdn.net/ld15102891672/article/details/8 ...

  6. Bootstrap Thumbnail

    Square Thumbnail with Image <!-- Square Thumbnail with Image --> <com.beardedhen.androidboo ...

  7. WPF元素绑定

    原文:WPF元素绑定 数据绑定简介:数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性.目标属性是依赖项属性.源对象可以是任何内容,从另一个WPF元素乃至ADO. ...

  8. sql 多列求和

    列相加即可注意Null不可加,先用ISNULL方法验证,设置默认值 SELECT ID, Name, Province, City, District, ISNULL(row1, 0), ISNULL ...

  9. gnuradio companion 找不到第三方模块gr-osmosdr的问题

    我使用了来自Ettus的gnuradio软件包,之后安装了gr-osmosdr 以在gnuradio中调用RTL电视棒. 但是在gnuradio companion找不到来自rtlsdr-source ...

  10. WM_CopyData 用法

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...