将sql文件传递给nodejs以便整体执行

时间:2017-07-21 17:52:43

标签: mysql sql node.js

我正在研究nodejs脚本,它接受包含大量sql语句的sql文件对mysql实例执行。到目前为止,我已经写了两个差异。 nodejs函数从指定的sql文件中一次读入一个sql语句(将每个sql语句存储在nodejs对象中并迭代以一次传递一个语句)并查询这些语句对mysql实例。这种方法适用于简单的SQL查询,但是,我需要运行的查询大而复杂(并且也由多人编写,因此结构各不相同)因此,一次运行这些复杂查询时,一次一个语句失败。

我偶然发现了针对nodejs的execsql包,我认为它将整个sql文件作为参数而不是一次查询一个语句(如果我错了请纠正我),但是我无法得到这个包工作(https://www.npmjs.com/package/execsql)。

var execsql = require('execsql'),
dbConfig = {
    host: 'cloud-address',
    user: 'db-user',
    port: 3306,
    password: 'user-pass'
},
sql = 'use test-db;',
sqlFile = __dirname + '/db.sql';
execsql.config(dbConfig)
.exec(sql)
.execFile(sqlFile, function(err, results){
    console.log(results);
}).end();

callback(err, results);
    ^ TypeError: callback is not a function  

任何帮助都应该被理解为什么nodejs包应该实现这一点(最终目标是使我能够传入一个sql文件,查询类似于通过mysql-workbench shell查询文件) )。

1 个答案:

答案 0 :(得分:0)

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace AutoCheckCheckbxIfFileExist_45245562
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            /*
             * I'm adding the fields dynamically through code so that
             * you know all the fields that are on my form.
             * Because they are created dynamically, I'll use 'Find' later on
             * to retrieve them.
             */
            addButton();
            addTextField();
            addChkbx();
        }

        private void addChkbx()
        {

            /*
             * This is the checkbox that will be dyamically checked or unchecked
             */
            CheckBox chkbx = new CheckBox();
            chkbx.Location = new Point(this.Location.X + 35, this.Location.Y + 55);
            chkbx.Name = "thechkbx";
            this.Controls.Add(chkbx);
        }

        private void addTextField()
        {
            /*
             * This is the field I'll be looking at to pull the file path
             */
            TextBox txtb = new TextBox();
            txtb.Name = "txtbx_thebox";
            txtb.Location = new Point(this.Location.X + 5, this.Location.X + 35);
            this.Controls.Add(txtb);
        }

        private void addButton()
        {
            /*
             * This will be the button you'll click on to get the check/uncheck behavior
             */
            Button btn = new Button();
            btn.Text = "ClickMe";
            btn.Click += Btn_Click;
            btn.Location = new Point(this.Location.X + 5, this.Location.Y + 5);
            this.Controls.Add(btn);
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            /*
             * This is the answer to your question
             * Because I dynamically create the fields, I'm using a Find to get them and use them.
             */
            ((CheckBox)this.Controls.Find("thechkbx", true)[0]).Checked = File.Exists(((TextBox)this.Controls.Find("txtbx_thebox", true)[0]).Text);


        }
    }
}

回调(错误,结果); va dendro de la promesa,recuerda que nodeesasíncrono