使用节点js和mysql插入动态值时获取错误:ER_PARSE_ERROR

时间:2015-12-24 14:33:21

标签: javascript mysql node.js

我正在尝试使用节点js将动态字段值插入到mysql数据库中。在我的服务器控制台中出现此错误:

{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near 
'[object Object]'f2a06305-7e98-4462-9ba2-72ce85fa4d32','undefined',
'59a3ff90-aa4a' at line 1]
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  index: 0 
}

错误是什么意思,我该如何解决?以下是服务器代码:

var mysql=require('mysql');
var http=require('http');
var uuid = require('node-uuid');
var path=require('path');
var express=require('express');
var app=express();
var bodyparser=require('body-parser');
app.use(bodyparser.json());       
app.use(bodyparser.urlencoded({extended: true})); 
var myconnection = mysql.createConnection({
host : "localhost",

user : "root",

password : "",

database : "siva"
});
app.use(express.static(__dirname + ""));
var uqid= uuid.v1();
var it_id=uuid.v4();
var tt=1;
var status="active";

   app.post("/insert",function(req,res){
        console.log(req.body);
        /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
        myconnection.query('Insert into cate_tbl (`cat_id`,`cat_name`,`cat_desc`,`cat_view_count`,`cat_status`) VALUES ("'+uqid+'","'+req.body.cat_name+'","'+req.body.cat_desc+'","'+tt+'","'+status+'")',function(err, results, fields) {
            //if (err) throw err;
            if (err){
                console.log("DB Error"+err); 
                res.send("add cate_tbl failed"+err);
            }else {
             generateQuery = function(data,tableName){
    var fields = Object.keys(data);

    var values = [];
    for(var o in data) {
        values.push(data[o]);
    }
    return "Insert into "+tableName+"("+fields.join("cat_it_id,cat_it_name,cat_pid,cat_it_count,cat_it_desc,cat_it_status")+") values ("+values.join("'"+it_id+"','"+req.body.item_name+"','"+uqid+"','"+req.body.tem_count+"','"+req.body.item_desc+"','"+status+"'")+")"
}

var query = generateQuery(req.body,"cate_item");

myconnection.query(query,function(err, results, fields) {
    //your logic.

     if(err)
      {
        console.log(err);
      }
      else
        {
          res.end(JSON.stringify(fields));
        }

 });
            }
        });
    });

app.get('/',function(req,res){
    res.sendfile("index.html");
});

app.listen(3000,function(){
    console.log("It's Started on PORT 3000");
})

0 个答案:

没有答案