命令行添加数据库数据

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);
}
                    })
                }
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值