// 数据库信息
var connection = mysql.createConnection({
host : ‘localhost’,
port:3306,
user : ‘root’,
password : ‘’,
database : ‘url’,
multipleStatements: true //这里一定要加上这个。先别管什么用,一定要加上。
});
因为我使用的是koa框架,所以用了async 、await 。这里不做理会,下面的代码才重要。
// 获取url
router.post(’/csdnurl’, async (ctx, next) => {
var start = (ctx.request.body.page - 1) * 10;
var sql = 'SELECT COUNT(*) FROM csdnurl ORDER BY id DESC; SELECT * FROM csdnurl ORDER BY id DESC limit ’ + start + ‘,10’;
let results = await query2(sql);
ctx.body = results
});
const query2 = function (sql) {
return new Promise((resolve, reject) => {
connection.query(sql, function (error, results) {
if (error) {
reject(error);
} else {
var allCount = results[0][0][‘COUNT(*)’];
var allPage = parseInt(allCount) / 10;
var pageStr = allPage.toString();
if (pageStr.indexOf(’.’) > 0) {
allPage = parseInt(pageStr.split(’.’)[0]) + 1;
console.log(allPage)
}
var List = results[1];
resolve(List)
}