簡體   English   中英

Node.js並使db-mysql工作

[英]Node.js and getting db-mysql to work

我很難學習node.js,盡管我對JavaScript非常了解,但是我無法使以下示例正常工作。 我想使用這個庫: http : //nodejsdb.org/

var mysql = require('db-mysql');
new mysql.Database({
    hostname: 'localhost',
    user: 'user',
    password: 'password',
    database: 'test'
}).on('error', function(error) {
    console.log('ERROR: ' + error);
}).on('ready', function(server) {
    console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();

2個問題:您需要在上面包裝什么代碼才能使其正常工作,以及如何設計一個簡單的節點應用程序

a)從網頁中檢索值b)將其存儲到mysql c)查詢此sql數據庫並顯示結果

a)參見http.get(options, callback)函數。

b,c)參見db-mysql query()文檔

例如(未測試):

//...
}).on('ready', function(server) {
  var mysql = this;
  http.get(options, function(res) {
    res.on('data', callback(data)) {
      // Parse the respsonse document here and extract data of interest.
      var data1, data2; //...
      // Insert the data into the database.
      mysql.query()
      .insert('mytable', ['column1', 'column2'], [data1, data2])
      .execute(function(error, result) {
        if (error) throw error;
        // Handle query result...
      });
    });                   
    });
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });
}).connect();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM