如何使用节点js将json数据插入到mysql中

时间:2018-04-07 17:58:28

标签: javascript mysql json node.js server

var express = require('express');
var app=express();
var length;
var affiliate = require('flipkart-affiliate');
var url = require('url');
var moment=require('moment');
var mysql = require('mysql');
var body;
var getUrl;
var product;
var offer;
var offer1;
var offer2;
var offer3;
var test1;
var test2;
var test3;
var title=[];
var description=[];
var startTime=[];
var endTime=[];
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'coupontest'
});
var client = affiliate.createClient({
    FkAffId: 'anandhkum',
    FkAffToken: 'eb030998c556443087d3b1a27ac569d0',
    responseType: 'json'
});
client.getCategoryFeed({
    trackingId: 'anandhkum'
}, function(err, result,getUrl){
    if(!err){
        body=JSON.parse(result);
        getUrl=body.apiGroups.affiliate.apiListings.food_nutrition.availableVariants["v1.1.0"].get;
        client.getProductsFeed({
            url: getUrl
        }, function(err, result){
            if(!err){

            }else {
                console.log(err);
            }
        });
    }
});
connection.connect(function(err) {
    if (err) {
        return console.error('error: ' + err.message);
    }
    console.log('Connected to the MySQL server.');
});
app.get('/',function (req,res) {
    client.getAllOffers(null,function(err, resp){
        if(!err){
            offer=JSON.parse(resp);
            test1=offer.allOffersList.length;
            res.send(offer);
            for(var i=0;i<test1;i++){
                description[i]=offer.allOffersList[i].description;
                startTime[i]=offer.allOffersList[i].startTime;
                endTime[i]=offer.allOffersList[i].endTime;
            }
            var stmt = "INSERT INTO offers (description,start_time,end_time) VALUES ?";
            connection.query(stmt, [description,startTime,endTime], function (err, result) {
                if (err) throw err.message;
                console.log("Number of records inserted: " + result.affectedRows);
            });
        }
        else{
            console.log(err);
        }
    });
});
app.listen(3000);
console.log("Listening to port 3000");

我收到了错误

 throw err; // Rethrow non-MySQL errors
        ^
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 '' 3 ports - multi device charging', 'Universal Voltage', 'Best Price Ever', 'Ext' at line 1

1 个答案:

答案 0 :(得分:1)

在执行预准备语句时,您需要为绑定的每个值#lang pl (require rackunit) (require racket/trace) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Question 1 (: min&maxRec (-> (Listof Number) Number Number (Listof Number))) (: min&max (-> Number Number Number Number Number (Listof Number) )) (define (min&max number1 number2 number3 number4 number5) ; gets all numbers and sends to the min&max recursive part (min&maxRec (list number2 number3 number4 number5) number1 number1)) ; made all numbers to become a list and has max and min numbers (define (min&maxRec myList max min) ;; logic: 1) if finished list give back result 2) else check if head of list max or min and recall the function with a shorter list and the new max or min (if (null? myList) ;return the min and max from myList when finished looking at all numbers (list min max) (if (> (first myList) max); is the head max? (min&maxRec (rest myList) (first myList) min) (if (< (first myList) min) ; is the head min? (min&maxRec (rest myList) max (first myList) ) (min&maxRec (rest myList) max min))))) (test (min&max 2 3 2 7 1) => '(1 7)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Question 2 a (: sublist-numbers (-> (Listof Any) (Listof Number))) (: tail-sublist-numbers (-> (Listof Any) (Listof Number) (Listof Number))) ;;This func calls a diff func since the tail-recursion needs an accumelator. (define (sublist-numbers myList) (tail-sublist-numbers myList `())) ;;this uses tail-recursion (all calculations are dont before the recursion and are sent with an accumelator) (define (tail-sublist-numbers myList acc) (if (null? myList) acc ; if finished all vars in list (if (number? (first myList)) (tail-sublist-numbers (rest myList) (cons (first myList) acc)) ; if the head of list is a number add it to acc, and continue working on the rest of the list (tail-sublist-numbers (rest myList) acc)))) ; else throw this var and work on rest of list 。例如。 ?

看看使用knex.js模块这样的东西可能是值得的。它使用下面的mysql模块并在底层进行sql绑定。