This repository has been archived on 2024-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
line-cost-js/Tools_MYSQLDBClass.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-03-22 11:58:40 +00:00
const mysql = require('mysql');
2021-04-24 13:18:34 +00:00
const dateFormat = require('dateformat');
2021-03-22 11:58:40 +00:00
/** Tools_MYSQLDB */
class Tools_MYSQLDBClass {
constructor() {
2021-04-24 13:18:34 +00:00
let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
2021-03-22 11:58:40 +00:00
let config = {
host: 'jianmiau.tk',
user: 'jianmiau',
password: 'VQ*ZetC7xcc9%dTW',
database: 'line-cost-php',
port: 3307
};
this.conn = new mysql.createConnection(config);
this.conn.connect(
function (err) {
if (err) {
2021-04-24 13:18:34 +00:00
console.log(`${datetime} !!! Cannot connect !!! Error:`);
2021-03-22 11:58:40 +00:00
throw err;
}
else {
2021-04-24 13:18:34 +00:00
console.log(`${datetime} jianmiau.tk Connect.`);
2021-03-22 11:58:40 +00:00
}
});
}
Query(Query) {
return new Promise((resolve, reject) => {
this.conn.query(Query,
function (err, results, fields) {
if (err) {
reject(err);
}
resolve(results);
});
})
}
CloseDB() {
this.conn.end(
function (err) {
if (err) throw err;
2021-04-24 13:18:34 +00:00
else console.log(`${datetime} Closing connection.`)
2021-03-22 11:58:40 +00:00
});
}
// readData() {
// this.conn.query('SELECT * FROM `LoveZhuHan` LIMIT 2',
// function (err, results, fields) {
// if (err) throw err;
// else console.log('Selected ' + results.length + ' row(s).');
// for (let i = 0; i < results.length; i++) {
// console.log('Row: ' + JSON.stringify(results[i]));
// }
// console.log('Done.');
// });
// // this.conn.end(
// // function (err) {
// // if (err) throw err;
// // else console.log('Closing connection.')
// // });
// }
}
module.exports = Tools_MYSQLDBClass