首先app.js里面
var getRouter = require('./routes/gets');//获取整张表
var getoneRouter = require('./routes/getone');;//根据条件获取
var postRouter = require('./routes/posts');增加
var delRouter = require('./routes/del');删除
var upRouter = require('./routes/updata');更新
app.use('/gets', getRouter);
app.use('/getone', getoneRouter);
app.use('/del', delRouter);
app.use('/postloop', postRouter);
app.use('/updata', upRouter);
routers文件夹下创建gets.js获取整张表数据的接口
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
let connection = mysql.createConnection({
host: "xxxxxx.com",
post: 3306,
user: "root",
password: "1234go",
database: 'ssc',
multipleStatements: true //允许执行多条语句
});
// console.log(88888)
connection.connect();
router.get('/', function(req, res) {
var value = req.query.table;
console.log(value);
var mysql = "select * from `" + value + "`";
console.log(mysql);
connection.query(mysql, function(err, result) {
//............
console.log(result)
res.send(result)
// connection.end();
// //回到主页
// res.redirect('/');
//关闭连接
});
})
module.exports = router;
根据条件获取数据
connection.connect();
router.get('/loop', function(req, res) {
console.log(req);
var name = req.query.name
var mysql = "select * from `loop` where name='" + name + "'";
console.log(mysql);
connection.query(mysql, function(err, result) {
//............
console.log(err)
console.log(result)
res.send(result)
// connection.end();
// //回到主页
// res.redirect('/');
//关闭连接
});
})
// var mysql = "select * from `loop` ";
var addSql = 'insert into loop(name,tel,content.ages) values(?,?,?,?)'
router.post('/loop', function(req, res) {
console.log(req);
var params = req.body;
var addSqlParams = [params.name, params.tel, params.content, params.ages]
connection.query(addSql, addSqlParams, function(err, result) {
//............
if (err) {
console.log(err.message);
return
}
// connection.end();
// //回到主页
// res.redirect('/');
//关闭连接
});
})
module.exports = router;
增加数据
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
let connection = mysql.createConnection({
host: "********.com",
post: 3306,
user: "root",
password: "1234go",
database: 'ssc',
multipleStatements: true //允许执行多条语句
});
console.log(88888)
connection.connect();
var mysql = "select * from `loop` ";
var addSql = 'insert into `loop` (name,tel,content,ages) values (?,?,?,?)'
router.post('/', function(req, res) {
console.log(req.body);
var params = req.body;
var addSqlParams = [params.name, params.tel, params.content, params.ages]
connection.query(addSql, addSqlParams, function(err, result) {
//............
if (err) {
console.log(err.message);
return
}
});
connection.query(mysql, function(err, result) {
if (err) {
console.log('[SELECT ERROR] - ', err.message);
return
}
res.send(result)
});
})
module.exports = router;
更新数据
var express = require('express');
var router = express.Router();
var URL = require('url');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '******.com',
user: 'root',
password: '1234go',
database: 'ssc'
});
connection.connect();
// var sql = 'SELECT * FROM `loop`';
// var addSql='INSERT INTO ssc_user(uname,riqi) VALUES(?,?)';
// var addSql = 'UPDATE ssc_user SET uname = ?,riqi = ? ';
router.post('/', function(req, res) {
// var params = req.body;
var id = req.body.id;
var name = req.body.name;
console.log(req.body)
connection.query("update `loop` set name='" + name + "' where id='" + id + "'", function(err, rows) {
// console.log(rows)
if (err) {
res.end('修改失败:' + err);
} else {
// res.redirect('/users');
res.send({ status: '200' })
}
});
});
module.exports = router;
删除数据
var express = require('express');
var router = express.Router();
var URL = require('url');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '****.com',
user: 'root',
password: '1234go',
database: 'ssc'
});
connection.connect();
// var sql='SELECT * FROM name';
// var addSql='INSERT INTO name(uname,sex) VALUES(?,?)';
router.get('/', function(req, res) {
var params = req.query; // get
var id = params.name;
console.log(id)
connection.query("delete from `loop` where name='" + id + "'", function(err, rows) {
if (err) {
res.end('删除失败:' + err)
} else {
// res.redirect('/users')
res.send({ status: '200' })
}
});
});
module.exports = router;
js中调用
$.ajax({//获取整张表
url: '/gets?table=' + value,
type: 'get',
dataType: 'json',
success: function(data) {
console.log(data)
},
error: function(err) {
console.log(err)
}
})
var value = $(this).text()
$.ajax({//根据条件查找
url: '/getone/loop?name=' + value,
type: 'get',
dataType: 'json',
success: function(data) {
console.log(data)
},
error: function(err) {
console.log(err)
}
})
$.ajax({//增加数据
url: '/postloop',
type: 'post',
data: {
'name': 'qqd',
'tel': '123456789',
'content': 'zhevhb',
'ages': '36'
},
dataType: 'json',
success: function(data) {
console.log(data)
},
error: function(err) {
console.log(err)
}
})
$.ajax({//修改数据
url: '/updata',
type: 'post',
dataType: 'json',
data: {
id: '6',
name: 'upname'
},
success: function(data) {
console.log(data)
},
error: function(err) {
console.log(err)
}
})
$.ajax({//删除数据
url: '/del?name=wanghui',
type: 'get',
dataType: 'json',
success: function(data) {
console.log(data)
},
error: function(err) {
console.log(err)
}
})