大家好,我是yma16,本期分享node连接mysql实现crud。
往期文章
node_windows环境变量配置
node_npm发布包
linux_配置node
node_nvm安装配置
node笔记_http服务搭建(渲染html、json)
node笔记_读文件
node笔记_写文件
小知识
热加载node,可以使用nodemon,保存后不用重启可以自动更新项目
$ npm install -g nodemon
调整项目的package.json的入口运行替换node为 nodemon
"scripts": {
"dev": "nodemon ./main/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
安装mysql模块
$ npm install mysql
连接mysql需要ip,port,username,password和database
示例:
var mysql= require('mysql');
# 创建连接
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
console.log('connection',connection)
# 关闭连接
connection.end()
连接我笔记本的mysql成功截图
本地数据库的csdn_demo
sql能实现数据增删改查,所以query可以实现crud的功能
传递sql建表语句
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
const createTableSql=`
CREATE TABLE table_by_node(id int AUTO_INCREMENT,title VARCHAR(255),body VARCHAR(255),PRIMARY KEY(id))
`
connection.query(createTableSql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
生成1条语句插入sql里面的table_by_node里面
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
let insertDataSql=""
for(let i=0;i<1;++i){
baseInsertSql=`INSERT INTO table_by_node(id,title,body) VALUES (${i}, 'node', 'study${i}');`
insertDataSql+=baseInsertSql
}
connection.query(insertDataSql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
动态生成99条语句插入sql里面的node_by_table里面
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
let values=[]
for(let i=1;i<100;++i){
values.push([i,'node插入数据动态','第'+i+'条数据'])
}
connection.query('INSERT INTO table_by_node(id,title,body) VALUES ?',[values], function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
写一个select 语句查询刚刚插入的数据表table_by_node
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
connection.query('select * from table_by_node;', function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
delete from table
where条件约束id删除一条
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
let deleteSql=`delete from table_by_node where id=1;`
connection.query(deleteSql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
delete之前使用sql的in子查询,查出多条数据再删除
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'csdn_demo'
});
let values=[]
for(let i=2;i<100;++i){
values.push(i)
}
let deleteSql=`delete from table_by_node where id in (${values.join(',')})`
connection.query(deleteSql, function (error, results) {
if (error) throw error;
console.log('The solution is: ', results);
});
connection.end()
感谢阅读,如有不足欢迎指出!