var mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database:'nodejs', port: 3306 }); conn.connect(); //查詢 // conn.query('SELECT * from user where name="wangzhiwei"', function(err, result) { // if (err) throw err; // console.log(result); // }) //新增 // conn.query('insert into user (username,password) values("huxiaona","123456")', function(err, result) { // if (err) throw err; // console.log(result); // }) //刪除 // conn.query('delete from user where username="wupeigui"', function(err, result) { // if (err) throw err; // console.log(result); // }) conn.query('update user set id="1" where username="huxiaona"',function(err,result){ if (err) {throw err}; console.log("修改數據成功"); }) conn.end();
有一點要指出的是,npm install mysql 的時候,要在程序里面,就算你把mysql加載到nodejs的node_modules文件夾里面也不頂事,可能是我操作不對吧,有知道的說下啊,謝謝,前端有了數據后感覺立馬吊吊的,一會就建一個網站管理系統玩去,簡單明了,不過nodejs要怎么才能用session,我百度了一下都說是用cookie來替換,這樣不太好吧