我正在使用带有node.js的MySql
我有这个查询并且它不起作用,MySQL中的temp1是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是浮动的我认为 - 我可以打印它,一切都很好:
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 :(得分: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 :(得分:0)
INSERT语句看起来不正确......
试试这个:
Abs() float64
答案 2 :(得分: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);
答案 3 :(得分:0)
插入一行并返回 insertId
的用例的一个工作示例:
函数定义:
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
const insertLog = async (db, requestUrl, requestType, isRequestSuccessful) => {
let insertId = null;
const stmt = 'insert into logs(request_url, request_type, request_status) values (?, ?, ?)';
const insertData = [requestUrl, requestType, isRequestSuccessful ? 'successful' : 'failed'];
db.query(stmt, insertData, (err, result, fields) => {
if (err) {
throw err;
}
insertId = result.insertId;
});
while(insertId === null) {
await sleep(100);
}
return insertId;
};
用法:
const logId = await insertLog(mysqlConnection, browseUrl, 'browse', false);
我使用的是 mysql 包 v2.18.1 和节点 v15.4.0。
应该很容易适应您的用例,例如
const insertId = await insert(mysqlConnection, data);