移动node.js数组结果到SQL数据库



我在github上找到了bluelinky,现在我想将一些数据存储到我的synology上的SQL数据库中。我可以查询我想要的数据,但我不知道如何构建它,并移动到一个数据库。

Console.log如下所示:

(
timeRaw:"160543",
开始:2022 - 12 - 22 t15:05:43.000z
结束:2022 - 12 - 22 t15:10:43.000z
时间:{驱动:5,空闲:1},
速度:{avg: 34,马克斯:57},
距离:2
}
{
timeRaw:"081630",
开始:2022 - 12 - 22 t07:16:30.000z
结束:2022 - 12 - 22 t07:20:30.000z
时间:{驱动:4,空闲:1},
速度:{avg: 41岁的马克斯:82},
距离:2
}
)

你知道如何把这个结果存入数据库吗?因为这个结果取决于我一天开车的次数,所以结果块可以是0,也可以是5。

感谢更新25 dec 2022:与此同时,我能够按照StefanBD的建议将数据平面化。唯一开放的是:如何将数据放入MariaDB数据库。

更新27 dec 2022:

我发现了如何在mariadb中输入值。现在我需要帮助从循环中导入结果。更具体地说,下面所述的结果需要移动到mariadb.

//Bluelinky verbinden
const BlueLinky = require("bluelinky");
const { isThisTypeNode } = require("typescript");

const client = new BlueLinky({
username: '*****************',
password: '*************',
brand: 'hyundai', // 'hyundai', 'kia'
region: '**', // 'US', 'EU', 'CA'
pin: '********'
});

// called when the client logs in successfully
client.on("ready", async () => {
const vehicle = client.getVehicle("**************");
//const trpInfo = await vehicle.tripInfo({year: new Date().getFullYear(), month: new Date().getMonth()+1, day: new Date().getDate()});
const trpInfo = await vehicle.tripInfo({year: 2022, month: 12, day: 4});
if (trpInfo != '') 
{

//Erstelle Datum
const tripdaydateraw = trpInfo[0].dayRaw.slice(4,6) + '/' + trpInfo[0].dayRaw.slice(6,8) + '/' + trpInfo[0].dayRaw.slice(0,4);
const tripdaydate = trpInfo[0].dayRaw.slice(0,4) + '-' + trpInfo[0].dayRaw.slice(4,6) + '-' + trpInfo[0].dayRaw.slice(6,8);
const tripdayname = new Date(tripdaydateraw).toLocaleDateString('de-de', {weekday: 'long'});
//Erstelle Anzahl der Fahrten
const tripdayCnt = trpInfo[0].tripsCount;
//Erstelle Tagesdistanz
const tripdayDistance = trpInfo[0].distance;
//Erstelle Tagedurchschnittsgeschwindigkeit
const tripdayavgSpeed = trpInfo[0].speed.avg;
//Erstelle maximale Tagesgeschwindigkeit
const tripdaymaxSpeed = trpInfo[0].speed.max;

// Errechne Anzahl der Fahrten
let i = 0, n = tripdayCnt - 1

while(i <= n) {
//Restliche Werte definieren
//Erstelle Durchschnittsgeschwindigkeit Fahrt
const tripavgSpeed = trpInfo[0].trips[i].speed.avg;
//Erstelle maximal Fahrgeschwindigkeit
const tripmaxSpeed = trpInfo[0].trips[i].speed.max;
//Startzeit
const strttimstamp = trpInfo[0].trips[i].start;
//Enzeit
const endtimstamp = trpInfo[0].trips[i].end;
//Startdatum
const strtdate = strttimstamp.toLocaleDateString();
//toLocaleTimeString konvertiert GMT + 1, Startzeit
const strtime = strttimstamp.toLocaleTimeString();
//Enddatum
const enddate = endtimstamp.toLocaleDateString();
//toLocaleTimeString konvertiert GMT + 1, Endzeit
const endtime = endtimstamp.toLocaleTimeString();
// Fahrzeit von Minuten in das Format hh:mm umwandeln
function toHoursAndMinutes(totalMinutes) {
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);

return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}`;
}

function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}

//Fahrzeit in Minuten
const drvtime = toHoursAndMinutes(trpInfo[0].trips[i].durations.drive);
//Distanz
const drvdistance = trpInfo[0].trips[i].distance
//Werte Ausgeben
//const tripinfodata = tripdaydate + ';' + tripdayCnt + ';' + tripdayDistance + ';' + tripdayavgSpeed + ';' + tripdaymaxSpeed + ';' + tripavgSpeed + ';' + tripmaxSpeed + ';' + strtime + ';' + endtime + ';' + drvtime + ';' + drvdistance + ';' + tripdayname + ';;';
const tripinfodata = tripdaydate + ',"' + tripdayCnt + '","' + tripdayDistance + '","' + tripdayavgSpeed + '","' + tripdaymaxSpeed + '","' + tripavgSpeed + '","' + tripmaxSpeed + '","' + strtime + '","' + endtime + '","' + drvtime + '","' + drvdistance + '","' + tripdayname + '";"";';
//console.log(strtime)


//SQL Statement bauen
pool.getConnection()
.then(conn => {
conn.query("INSERT INTO nightfury_trips (tripdaydate, tripdayCnt, tripdayDistance, tripdayavgSpeed, tripdaymaxSpeed, tripavgSpeed, tripmaxSpeed, strtime, endtime, drvtime_minutes, drvdistance, tripdayname) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", [tripdaydate, tripdayCnt, tripdayDistance, tripdayavgSpeed, tripdaymaxSpeed, tripavgSpeed, tripmaxSpeed, strtime, endtime, drvtime_minutes, drvdistance, tripdayname])
.then((rows) => {
console.log(rows); //[ {val: 1}, meta: ... ]
//pool.end();
conn.end();
})
.catch(err => {
//handle error
console.log(err);
//pool.end();
conn.end();
});
}).catch(() => {
//not connected
pool.end();
//conn.end();
});
console.log(tripinfodata)
i += 1;}};
});

更新28.12.2022

我做到了。只需要在循环行结束前添加insert语句,它就可以工作了;池中。结束命令需要在循环后添加。

更新01.01.2023添加了缺失的代码

更新28.12.2022我做到了。只需要在循环行结束前添加insert语句,它就可以工作了;池中。结束命令需要在循环后添加。

相关内容

  • 没有找到相关文章

最新更新