NodeJS中的mssql无法插入MySQL表

时间:2019-08-15 09:04:11

标签: mysql node.js sql-server post insert

我必须在NodeJS的MySQL表中插入值。我正在使用mssql依赖项来做到这一点。

app.post('/add_register_request', function(req,res){
    console.log("ADDDD")
    var request = req.body
    console.table(request)
    if(request['category']=='student'){
        var sql = `INSERT INTO students VALUES (${request['name']}, ${request['username']}, ${request['class']}, ${request['section']}, ${request['admission_no']}, ${request['club']}, ${request['vpa']});`
    }else if (request['category']=='teacher'){
        var subjects = request['subjects'];
        subjects = Array(subjects)
        var sql = `INSERT INTO teachers VALUES (${request['name']}, ${request['username']}, ${request['class_teacher']}, ${subjects}, ${request['teacher_id']})`
    }else{console.log('ERROR')}
    console.log(sql)
    var config = {
        host: "localhost",
        port: "3306",
        database: "wizlearn",
        user: "root",
        password: "xxxx"
    };
    mysql.connect(config, function (err) {
        var query = new mysql.Request()
        query.query(sql, function(err,recordset){console.log("sql sent")})
    })
    mysql.close()
})

请求对象是:-

{ 名称:“ Ravi Sundar”,   用户名:“ ravisundar”,   class_teacher:'7B',   Teacher_id:“ 12345”,   主题:['7B English'],   类别:“老师” }

问题是,该语句未在执行,即未插入值。 我要执行构建的SQL语句。我该怎么做呢? 预先感谢!

0 个答案:

没有答案