Node操做Mysql的簡單例子

先裝包mysql

npm install mysql

簡單的例子sql

var mysql = require('mysql')
//建立鏈接
var connection = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'08186912',
    database:'idea' //要鏈接的數據庫名稱
});
//鏈接數據庫
connection.connect();
//執行操做
connection.query('SELECT * from users',function(error,results,fields) {
    if(error) throw error;
    console.log('結果',fields);
    console.log('結果',results[0].username);
})
//關閉鏈接
connection.end()
相關文章
相關標籤/搜索