【问题标题】:How to insert HTTP query parameters into azure sql db using azure functions如何使用 azure 函数将 HTTP 查询参数插入 azure sql db
【发布时间】:2018-07-09 16:39:49
【问题描述】:

我有一个包含三个查询参数的天蓝色函数,我想将它们插入 Azure SQL db。下面是我编写的代码,但是,我收到一个错误,提示无效列。选择查询工作正常。

const Connection = require('tedious').Connection;
const Request = require('tedious').Request;

module.exports = function (context, req) {
 context.log('JavaScript HTTP trigger function');
 let bId = req.query.binId
 let bIP = req.query.IPAddress
 let btype =req.query.bincat

 context.log(bId);
 context.log(bIP);
 context.log(btype);

 const config = {
    userName: 'used the necessary input',
    password: 'used the necessary input',
    server: 'used the necessary input',
    options: {
        database: 'used the necessary input',
        encrypt: true
    }
};
if (bId && btype && bIP) {
    context.res = {
        body: "Details: " + (bId) + ","+(bIP)+","+(btype) 
    };
}
else {
    context.res = {
        status: 400,
        body: "Please pass a name on the query string"
    };
}


context.log('config done');
const connection = new Connection(config);
connection.on('connect', err => {
    err ? context.log(err) : executeStatement();
});
context.log('connection done');
const query = 'insert into Bins(BinId, IPAddress, BinType) values (bId,bIP, btype)';
//const query = 'select * from Bins';
const executeStatement = () => {
    const request = new Request(query, (err, rowCount) => {
        context.log('inside exe');
        err ? context.log(err) : context.log(rowCount);
    });
    request.on('row', columns => {
        columns.forEach(column => context.log(column.value));
    context.done();
    });
connection.execSql(request);
};
 context.log('executed statement');
 //context.done();
// context.done();
};

错误在插入命令中。

【问题讨论】:

    标签: javascript azure-sql-database azure-functions


    【解决方案1】:

    您的参数缺少request.addParameter(...,您的参数也应以@ 开头。有关更多详细信息,请参阅文档https://tediousjs.github.io/tedious/parameters.html

    创建请求对象后,你需要做一些类似的事情

    var TYPES = require('tedious').TYPES
    
    const query = 'insert into Bins(BinId, IPAddress, BinType) values (@bId,@bIP, @btype)';
    const request = new Request(query, .....
    
    request.addParameter('bId', TYPES.Int, bId);
    request.addParameter('bIP', TYPES.VarChar, bIP);
    request.addParameter('btype', TYPES.VarChar, btype);
    
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2021-10-30
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多