安装指令:npm install mysql
var mysql = require("mysql");
console.log(mysql);
// 创建链接对象
var conn = mysql.createConnection({
host:‘localhost‘,
port:3306,
user:"root",
password:‘root‘,
database:"wh1807"
})
// 链接数据库
conn.connect((err)=>{
if(err) throw err;
console.log("数据库链接成功!")
});
var insertSql = "insert into user1807 (username,password) values (?,?)";
var insertParams = [‘dazuozuo‘,‘daydayup‘];
conn.query(insertSql,insertParams,(err,result)=>{
if(err) throw err;
console.log("insert ++++++++++++++++++++++++++++++++");
// response.write(result); 返回浏览器
console.log(result);
})
var selectSql = "select * from user1807 where id > ? "; // ? 需要的参数 防止SQL注入
conn.query(selectSql,2,(err,result)=>{
if(err) throw err;
console.log("select ++++++++++++++++++++++++++++++++");
// response.write(result); 返回浏览器
console.log(result);
})
var updateSql = "update user1807 set password = ? where username = ? ";
var updateParams = [‘wuhan180777777‘,‘dazuozuo‘ ];
conn.query(updateSql,updateParams,(err,result)=>{
if(err) throw err;
console.log("update ++++++++++++++++++++++++++++++++");
// response.write(result); 返回浏览器
console.log(result);
});
var deleteSql = "delete from user1807 where username = ?";
conn.query(deleteSql,‘dazuozuo‘,(err,result)=>{
if(err) throw err;
console.log("delete ++++++++++++++++++++++++++++++++");
// response.write(result); 返回浏览器
console.log(result);
})
// 关闭数据库
conn.end(()=>{
console.log("数据库已经关闭了....")
})
补充菜鸟教程:(1)连接数据库var mysql = require(‘mysql‘);var connection = mysql.createConnection({ host : ‘localhost‘, //主机 user : ‘root‘, //用户名 password : ‘123456‘, //密码 database : ‘test‘ //数据库名});connection.connect();connection.query(‘SELECT 1 + 1 AS solution‘, function (error, results, fields) { if (error) throw error; console.log(‘The solution is: ‘, results[0].solution);});(2)查询数据var mysql = require(‘mysql‘); var connection = mysql.createConnection({ host : ‘localhost‘, user : ‘root‘, password : ‘123456‘, port: ‘3306‘, database: ‘test‘ }); connection.connect();var sql = ‘SELECT * FROM websites‘;//查connection.query(sql,function (err, result) { if(err){ console.log(‘[SELECT ERROR] - ‘,err.message); return; } console.log(‘--------------------------SELECT----------------------------‘); console.log(result); console.log(‘------------------------------------------------------------\n\n‘); });connection.end();(3)插入数据var mysql = require(‘mysql‘); var connection = mysql.createConnection({ host : ‘localhost‘, user : ‘root‘, password : ‘123456‘, port: ‘3306‘, database: ‘test‘ }); connection.connect();var addSql = ‘INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)‘;var addSqlParams = [‘菜鸟工具‘, ‘https://c.runoob.com‘,‘23453‘, ‘CN‘];//增connection.query(addSql,addSqlParams,function (err, result) { if(err){ console.log(‘[INSERT ERROR] - ‘,err.message); return; } console.log(‘--------------------------INSERT----------------------------‘); //console.log(‘INSERT ID:‘,result.insertId); console.log(‘INSERT ID:‘,result); console.log(‘-----------------------------------------------------------------\n\n‘); });connection.end();(4)更新数据var mysql = require(‘mysql‘); var connection = mysql.createConnection({ host : ‘localhost‘, user : ‘root‘, password : ‘123456‘, port: ‘3306‘, database: ‘test‘ }); connection.connect();var modSql = ‘UPDATE websites SET name = ?,url = ? WHERE Id = ?‘;var modSqlParams = [‘菜鸟移动站‘, ‘https://m.runoob.com‘,6];//改connection.query(modSql,modSqlParams,function (err, result) { if(err){ console.log(‘[UPDATE ERROR] - ‘,err.message); return; } console.log(‘--------------------------UPDATE----------------------------‘); console.log(‘UPDATE affectedRows‘,result.affectedRows); console.log(‘-----------------------------------------------------------------\n\n‘);});connection.end();