2015-10-20 58 views
2

我使用MySQL與node.js的INSERT INTO失敗節點的mysql - 變量始終是NULL

我有這個疑問,它不工作,在的temp1 MySQL是alwayls NULL:

var queryTemp1 = { tid: '1', temp: temp1, timestamp: myDate }; 
con.query('INSERT INTO temps SET ?', queryTemp1, function(err,res){ 
    if(err) throw err; 

    console.log('Last insert ID:', res.insertId); 

}); 

temp1中是浮動我認爲 - - 我可以打印出來,一切都OK:

var temp1 = parseFloat(stdout); 
var temp1 = temp1/1000 
var temp1 = (temp1.toFixed(2)); 

我的表中的列:

+-----------+-------------+------+-----+---------+-------+ 
| Field  | Type  | Null | Key | Default | Extra | 
+-----------+-------------+------+-----+---------+-------+ 
| tid  | smallint(6) | YES |  | NULL |  | 
| timestamp | datetime | YES |  | NULL |  | 
| temp  | float(4,2) | YES |  | NULL |  | 
+-----------+-------------+------+-----+---------+-------+ 

我在做什麼錯?

+0

你不能只是'設置1'。設置** WHICH **字段? –

+0

NULL在temp字段 – user1815887

+0

那麼你需要'set temp =?'。你不能只設置一個值。值已經「設置」。 –

回答

0

tid目前是一個字符串,它尋找一個tinyint,請嘗試單引號,看看是否會引發火災。

另外,還要確保你的日期時間日期時間實際上不是一個時間戳,即像2014-01-01 00:00:00而不是1231232131

編輯:在這裏你的代碼可以擦洗值,

var temp1 = parseFloat(stdout); 
var temp1 = temp1/1000 
var temp1 = (temp1.toFixed(2)); 

,如果您需要更新temp1,取出var

var temp1 = parseFloat(stdout); 
temp1 = temp1/1000 
temp1 = (temp1.toFixed(2)); 
+1

我試過了,沒有引號,沒有變化。時間戳實際上是日期時間 - 它運行良好,並在MySQL中存儲日期 – user1815887

+0

您是否可以檢查tid是否實際上不是主鍵? –

+1

此表中沒有主鍵。 – user1815887

0

這INSERT語句不看的權利...

試試這個:

// BIND PARAMS 
var queryTemp1 = { tid: '1' 
       , temp: temp1 
       , timestamp: myDate 
       }; 

// VARIABLE FOR SQL 
var sql = "INSERT INTO temps(tid, temp, timestamp)\ 
      VALUES (:tid, :temp, :timestamp)"; 

// DATABASE 
con.query(sql 
     , queryTemp1 
     , function(err, res) { 
      if(err) throw err; 
      console.log('Last insert ID:', res.insertId); 
      }); 
+0

我想要插入所有的值,並且解析提供的loks只想插入temp。 – user1815887

+0

刷新一下 - 我編輯了答案:) – doublesidedstickytape

+0

看看MySQL文檔 - 看起來好像你可以使用INSERT SET http://dev.mysql.com/doc/refman/5.6/en/insert。 html。我從來沒有用過它!無論如何 - 我不確定它適合綁定變量。看看我的回答是否有助於你,如果你需要更多幫助,請隨時給我留言。 – doublesidedstickytape

0

這裏是整個代碼:

var mysql = require("mysql"); 
    var child_process = require('child_process'); 
    var temp1; 
    // First you need to create a connection to the db 
    var con = mysql.createConnection({ 
     host: "localhost", 
     user: "user", 
     password: "password", 
     database: "database" 
    }); 

    con.connect(function(err){ 
     if(err){ 
     console.log('Error connecting to Db'); 
     return; 
     } 
     console.log('Connection established'); 
    }); 

    console.log(temp1); 

    child_process.exec("sudo -u www-data grep -a 't=' /var/www/temp/w1_slave | cut -f2 -d=", function (err, stdout, stderr){ 
     if (err) { 
      console.log("child processes failed with error code: " + 
       err.code); 
     } 
     console.log(stdout); 
     temp1 = parseFloat(stdout); 
     temp1 = temp1/1000 
     temp1 = (temp1.toFixed(2)); 
    }); 



    var moment = require('moment'); 
    var now = moment(); 
    myDate = moment(now.format('YYYY/MM/DD HH:mm:ss')).format("YYYY-MM-DD HH:mm:ss"); 

    var queryTemp1 = [1, temp1]; 

    // VARIABLE FOR SQL 
    var sql = "INSERT INTO temps(tid, temp) VALUES (?)"; 

    // DATABASE 
    con.query(sql 
      , queryTemp1 
      , function(err, res) { 
       if(err){console.log(queryTemp1); throw err;} 
       console.log('Last insert ID:', res.insertId); 
       }); 
    console.log(queryTemp1); 
+0

用SET代替VALUES - 如果你想使用VALUES,那麼你需要規定它們是什麼(如我的答案所示) – doublesidedstickytape