const fs = require('fs');
const mysql = require('mysql');
function query(sql, params, mysql_host, mysql_user, mysql_pass, mysql_db, mysql_port, callback) {
var connection = mysql.createConnection({
multipleStatements: true,
host: mysql_host,
user: mysql_user,
password: mysql_pass,
database: mysql_db,
port: mysql_port
});
connection.connect(function (err) {
if (err) {
console.log(err);
throw err;
}
connection.query(sql, params, function (err, results, fields) {
if (err) {
console.log(err);
throw err;
} else {
callback(err, results, fields);
}
connection.end(function (err) {
console.log(err);
throw err;
}
});
});
});
};
const args = process.argv.splice(2);
let [mysql_host, mysql_user, mysql_pass, mysql_db, mysql_port, importUrl] = ['', '', '', '', '',''];
args.forEach(item => {
switch (item) {
case '-h':
mysql_host = args[args.indexOf('-h') + 1];
case '-u':
mysql_user = args[args.indexOf('-u') + 1];
case '-p':
mysql_pass = args[args.indexOf('-p')+ 1];
case '-d':
mysql_db = args[args.indexOf('-d') +1];
case '-o':
mysql_port = args[args.indexOf('-o') + 1];
case '-f':
importUrl = args[args.indexOf('-f') + 1];
}
});
if (args.length == 1 && args[0] == '-H') {
console.log('Usage :Some parameters for method implantation into the standard method library are described below.')
console.log(' ');
console.log('options : ');
console.log(' ');
console.log(' -h : Database host');
console.log(' -u : Database user name ');
console.log(' -p : Database user password ');
console.log(' -d : Database name');
console.log(' -o : Database port');
console.log(' -f : Path to the imported method file');
} else {
var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
if (h !== '-h' || u !== '-u' || p !== '-p' || d !== '-d' || o !== '-o' || f !== '-f') {
console.log("请检查命令行参数");
} else {
fs.exists(importUrl, function (exists) {
if (!exists) {
console.log("文件不存在");
} else {
const methodInfo = JSON.parse(fs.readFileSync(importUrl, 'utf8'));
const methodName = methodInfo.methodName ? methodInfo.methodName : '';
const methodType = methodInfo.methodType ? methodInfo.methoType : '';
const paramDetail = methodInfo.paramDetail ? methodInfo.paramDetail : '';
if (!methodName || !methodType || !paramDetail) {
console.log('请检查导入方法文件的字段参数');
} else {
const sql = 'insert into classic_standard_method (methodName,methodType,paramDetail) values(?,?,?)';
query(sql, [methodName, methodType, JSON.stringify(paramDetail)], mysql_host, mysql_user, mysql_pass, mysql_db, mysql_port, function (err, rows) {
if (err) {
console.log(err);
}
})
}
}
命令行添加数据库数据
最新推荐文章于 2024-07-25 14:30:53 发布