nodejs 與 mysql聯接

首先安裝Mysql 模塊吧javascript

npm install mysql

剛開始在網上搜索了一個測試代碼,發現根本就鏈接不上mysql.java

varClient=require('mysql').Client,
client =new Client(),mysql

相似這樣的.sql

查緣由: npm

console.log(require("mysql"));測試

對象只有5個方法ui

createConnectionspa

createPool對象

createQueryblog

escape

escapeId

沒有Client 方法和 屬性.

去mysql 模塊目錄查看了下 Readme.md

#Here is an example on how to use it:

```js
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
});

connection.connect();

看來網上的代碼是有問題.應該是版本更新了不支持了.本身從新寫了測試代碼運行正常

// mysql.js
//加載mysql Module
var Client = require('mysql');

var db_options = {
    host: "localhost",
    port: 3306,
    user: "root",
    password: "123456",
    database: "drupal"
};
var client = Client.createConnection(db_options);

client.connect();

client.query(
  'SELECT * FROM actions',
  function selectCb(err, results, fields) {
    if (err) {
      throw err;
    }

    console.log(results);
    console.log(fields);
  }
);
client.end();
相關文章
相關標籤/搜索