腾讯云的mysql怎么用 腾讯云服务器部署mysql( 三 )


var employeeDeleteSql_Params = 'Peter';
connection.query(employeeDeleteSql,employeeDeleteSql_Params,function(err,result){
if(err) console.log('[DELETE ERR]-',err.message);
console.log(result);
})
}deleteEmployee();
这样我们就可以愉快的操作数据库了
如果大家想把项目更模块化操作,可在model下面新建一个employee_two.js,代码如下
var mysql = require('mysql');var connection = mysql.createConnection({
host:'',
user:'root',
password:'',
database:'nodejs',
port:})connection.connect();/**
* 增加员工
* @param {String} employee
* @param {Function} callback
*/exports.addEmployee=function(employee,callback){
var employeeInsertSql = 'INSERT INTO employee(name,sex,age,email) VALUES(?,?,?,?)';
var employeeInsertSql_Params = [employee.name,employee.sex,employee.age,employee.email]
connection.query(employeeInsertSql,employeeInsertSql_Params,callback)}/**
* 更新员工
* @param {String} employee
* @param {Function} callback
*/exports.updateEmployee = function(name,age){
var employeeUpdateSql = "UPDATE employee SET name = ? WHERE age =?";
var employeeUpdateSql_Params = ['Peter',22];
connection.query(employeeUpdateSql,employeeUpdateSql_Params,function(err,result){
if(err) console.log('[UPDATE ERR]-',err.message);
console.log(result);
})}/**
* 查询员工
*
*/exports.getEmployee = function(){
var employeeGetSql = "SELECT * FROM employee";
connection.query(employeeGetSql,callback)}/**
* 删除员工
* @param {String} name
*/exports.deleteEmployee = function(name){
var employeeDeleteSql = "DELETE employee WHERE name = ?";
var employeeDeleteSql_Params = 'Peter';
connection.query(employeeDeleteSql,employeeDeleteSql_Params,callback)
}再在根目录下新建index.js,代码如下var db = require('./model/employee_two');//增加员工var employee = {
name:'lisa',
age:22,
sex:"女",
email:'99533212@qq.com'}db.addEmployee(employee,function(err,result){
if(err) console.log("[INSERT err]-",err.message)
console.log(result);})//删除员工db.deleteEmployee('Peter',function(err,result){
if(err) console.log("[DELETE err]-",err.message)
console.log(result);})//更新员工db.updateEmployee('Tim',23,function(err,result){
if(err) console.log("[UPDATE err]-",err.message)
console.log(result);})//查询员工db.getEmployee(function(err,result){
if(err) console.log("[GET err]-",err.message)
console.log(result);})
【腾讯云的mysql怎么用 腾讯云服务器部署mysql】关于腾讯云的mysql怎么用和腾讯云服务器部署mysql的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读