js怎么直连mysql js直接连接mysql( 三 )


*/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);})
javascript中如何连接mysql数据库JS访问数据库应该是可以的,前提是你必须把网页保存成*.hta格式 。html格式的文件没有足够的权限 。
楼主如果想在网页中访问数据库,就不要使用JS吧,PHP/ASP/JSP都行 。
如果楼主想做一个本地使用的程序访问数据库,那只要把页面存成hta就行了 。
关于js怎么直连mysql和js直接连接mysql的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读