注意事项:
1、需要安装依赖包mysql
(1)进入当前项目目录
(2)初始化创建package.json文件:
npm init -y
(3)安装mysql包:
npm install mysql
(4)使用时导入包:
var mysql = require('mysql');
2、MySQL数据库的密码加密方式如果是加强型加密时,需要修改成弱型加密方式(
修改方法为:launchpad-系统偏好设置-MySQL-Stop MySQL Server-Initialize Database-Use Leagacy Password Encryption)
连接数据库(connectMySQL.js)
var mysql = require('mysql');
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'zsy0123456.',
port:'3306',
database:'personDatabase'
});
connection.connect();
console.log("mysql has connnected");
connection.end();
console.log("mysql has colsed");
操作数据库
操作数据表
操作数据
1、插入数据(insertMySQL.js)
var mysql = require("mysql");
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'zsy0123456.',
port:'3306',
database:'personDatabase'
});
connection.connect();
console.log("连上数据库");
var sqlInsert = "insert into personTable (id,name,gender,age) values (?,?,?,?)";
var sqlValues = [1002,'uiChen','female',26];
connection.query(sqlInsert, sqlValues, function(error,result){
if (error) {
console.log("insert error:", error.message);
return;
};
console.log("insert success:", result);
});
connection.end();
console.log("断开数据库连接");
![](https://i-blog.csdnimg.cn/blog_migrate/a844c83b7b774cd65c71d3197addb0fa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d795223e31f818a3c77044a066760681.png)
2、查询数据(selectMySQL.js)
var mysql = require("mysql");
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'zsy0123456.',
port:'3306',
database:'personDatabase'
});
connection.connect();
console.log("连上数据库");
var sqlSelect = "select * from personTable";
connection.query(sqlSelect, function(error,result){
if (error) {
console.log("select error:", error.message);
return;
};
console.log("select success:", result);
});
connection.end();
console.log("断开数据库连接");
![](https://i-blog.csdnimg.cn/blog_migrate/4d8367e65873a7486a8bafd377fabfd8.png)
3、更新数据(updateMySQL.js)
var mysql = require("mysql");
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'zsy0123456.',
port:'3306',
database:'personDatabase'
});
connection.connect();
console.log("连上数据库");
var sqlUpdate = "update personTable set name=? where id=?";
var sqlValues = ['testLuo',1002];
connection.query(sqlUpdate, sqlValues, function(error,result){
if (error) {
console.log("update error:", error.message);
return;
};
console.log("update success:", result);
});
connection.end();
console.log("断开数据库连接");
![](https://i-blog.csdnimg.cn/blog_migrate/7908669fcfd5896f451f8f3439b65180.png)
![](https://i-blog.csdnimg.cn/blog_migrate/90e07f7df3f9e535f3b4c4525af8360d.png)
4、删除数据(deleteMySQL.js)
var mysql = require("mysql");
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'zsy0123456.',
port:'3306',
database:'personDatabase'
});
connection.connect();
console.log("连上数据库");
var sqlDelete = "delete from personTable where id=?";
var sqlValues = [1002];
connection.query(sqlDelete, sqlValues, function(error,result){
if (error) {
console.log("delete error:", error.message);
return;
};
console.log("delete success:", result);
});
connection.end();
console.log("断开数据库连接");
![](https://i-blog.csdnimg.cn/blog_migrate/c356a384126dde4624bb242c3e5bb1d6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b73dce0811575226aee2dbd418de9de2.png)
异常处理: