nodejs操作mysql创建库和表_NodeJs操作mysql数据库(js版)

/**

* Created by wangyc on 2015-3-18.

* timetrack.js

*/

var qs = require('querystring');

exports.sendHtml = function (res, html) {

res.setHeader('Content-Type', 'text/html');

res.setHeader('Content-Length', Buffer.byteLength(html));

res.end(html);

};

exports.parseReceivedData = function (req, cb) {

var body = '';

req.setEncoding('utf8');

req.on('data', function (chunk) {

body += chunk;

});

req.on('end', function () {

var data = qs.parse(body);

cb(data);

});

};

exports.actionForm = function (id, path, label) {

var html = '

' +

'' +

'' +

'

';

return html;

};

exports.add = function (db, req, res) {

exports.parseReceivedData(req, function (work) {

db.query(

"insert into work(hours,date,description) " +

"values(?,?,?)",

[work.hours, work.date, work.description],

function (err) {

console.log(err);

console.log(res);

if (err) throw err,

exports.show(db, res);

}

);

});

};

exports.delete = function (db, req, res) {

exports.parseReceivedData(req, function (work) {

db.query(

"delete from work where id=?",

[work.id],

function (err) {

if (err) throw err;

exports.show(db, res);

}

);

});

};

exports.archive = function (db, req, res) {

exports.parseReceivedData(req, function (work) {

db.query(

"update work set archived=1 where id=?",

[work.id],

function (err) {

if (err) throw err;

exports.show(db, res);

}

);

});

};

exports.show = function (db, res, showArchived) {

var query = "select * from work where archived=? order by date desc ";

var archiveValue = showArchived ? 1 : 0;

db.query(

query,

[archiveValue],

function (err, rows) {

if (err) throw err;

html = showArchived ? ''

: 'Archived work
';

html += exports.workHitlistHtml(rows);//将结果格式化为表格

html += exports.workFormHtml();

exports.sendHtml(res, html);//给用户发送HTML响应

}

);

};

exports.showArchived = function (db, res) {

exports.show(db, res, true);

};

exports.workHitlistHtml = function (rows) {

var html = '

console.log(rows)

for (var i in rows) {

html += '

';

html += '

' + rows[i].date + '';

html += '

' + rows[i].hours + '';

html += '

' + rows[i].description + '';

if (!rows[i].archived) {

html += '

' + exports.workArchiveForm(rows[i].id) + '';

}

html += '

' + exports.workDeleteForm(rows[i].id) + '';

html += '

';

}

html += '

';

return html;

};

exports.workFormHtml = function () {

var html = '

'

Date(YYYY-MM-DD):

' +

'

Hours worked:

' +

'

Description:
' +

'

' +

'' +

'

';

return html;

};

exports.workArchiveForm = function (id) {

return exports.actionForm(id, '/archive', 'Archive');

};

exports.workDeleteForm = function (id) {

return exports.actionForm(id, '/delete', 'Delete');

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值