2 回答
TA贡献1785条经验 获得超8个赞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
接着使用nodejs对数据库进行增删改查:
//引入mysql模块 var mysql = require('mysql'); //链接数据库 var connection = mysql.createConnection({ host:'localhost', user:'root', password:'root', database:'node', }) connection.connect(); //查询数据 var sql = 'SELECT * FROM user'; connection.query(sql,function (err,result) { if(err){ console.log('error'); return; } console.log('-----------------查询----------------'); console.log(result); console.log('-----------------查询结束----------------'); })
//增加数据 var addsql = 'INSERT INTO user(name,age,sex) VALUES(?,?,?)'; var addsqlparams = ['汪丹萍','24','女']; connection.query(addsql,addsqlparams,function (err,result) { if(err){ console.log('error'); return; } console.log('-----------------新增成功----------------'); console.log(result); console.log('-----------------结束----------------'); })
//修改数据 var modsql = 'UPDATE user SET name = ?,age = ? WHERE id = ?'; var modsqlparams = ['吕雪源love','26','1']; connection.query(modsql,modsqlparams,function (err,result) { if(err){ console.log('err'); return; } console.log('--------------------------------'); console.log(result); console.log('--------------------------------'); })
//删除数据 var delsql = 'DELETE FROM user where id = 2'; connection.query(delsql,function (err,result) { if(err){ console.log('err'); return; } console.log('----------删除-------------'); console.log(result); })
connection.end(); |
- 2 回答
- 0 关注
- 494 浏览
添加回答
举报