Postgres语法错误。节点。使用Javascript

时间:2014-07-22 23:05:48

标签: javascript node.js postgresql

我正在尝试向数据库输入一组asn前缀数据,并且我一直收到postgres语法错误。这个问题让我完全不知所措。这是代码,我将数字列表拉入并将它们传递给数组。然后将它们输入到一个循环中,该循环将为包含前缀数据的表格搜索网站。每个As号都可以使用,直到81号。

var request = require('request');
var cheerio = require('cheerio')
var pg = require('pg');
var squel=require('squel');
var conString="postgres://postgres:Password@localhost:5433/postgres"
var client = new pg.Client(conString);
var newArr = [];

client.connect(function (err,data){
    if(err) console.log("'Error connecting to PG'", err);
    else{
    var query = squel.select().field("asnumber").from('"asntable"').limit(130);
    // console.log(query.toString());
    client.query(query.toString(), function (err,res){
        if (err) throw err;
        var x = JSON.stringify((res.rows));
        var y = x.split(",");
            for (i in y){
            newArr.push(Number(y[i].replace(/\D/g,'')))
            }
         console.log(newArr);  

 for (apn in newArr) {
                var options = {
                    url : 'http://bgp.he.net/AS'+newArr[apn]+'#_prefixes',
                    headers:  {
                        'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
                    }
                };
                request(options, (function(apn) { return function(err, resp, body) {
                        var $ = cheerio.load(body);
                        var x = $('#table_prefixes4');
                        console.log("*"+x.text());
                            $('#table_prefixes4 tr').each(function(index, prefix) {
                            $(this).find('.nowrap').each(function(){
                            event = $(this).text().trim();
                            nextevent = $(this).next().text().trim();
                            console.log(apn+","+event+","+nextevent)
                            var intoquery = squel.insert().into("secondastable").set("asnumber",newArr[apn]).set("prefixes",event).set("description",nextevent);
                            console.log(intoquery.toString())
                            client.query(intoquery.toString());
                            });        
                            });
                }})(apn))  
            }

        });
   }
}); 

问题是我在之前的0-80和81之间找不到明显的区别。这是它给我的错误。

    error: syntax error at or near "s"
    at Connection.parseE (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:526:11)
    at Connection.parseMessage (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:356:17)
    at Socket.<anonymous> (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:105:22)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:748:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:410:10)
    at emitReadable (_stream_readable.js:406:5)
    at readableAddChunk (_stream_readable.js:168:9)
    at Socket.Readable.push (_stream_readable.js:130:10)

我已经在下面重写了它,所以如果你有postgres,你可以在没有数据库的情况下设置它。任何线索都会非常感激。

var request = require('request');
var cheerio = require('cheerio')
var pg = require('pg');
var squel=require('squel');
var conString="postgres://postgres:Password@localhost:5433/postgres"
var client = new pg.Client(conString);
var newArr = [];
a = [81]        
client.connect(function (err,data){
    if(err) console.log("'Error connecting to PG'", err);
    else{
            for (apn in a) {
                var options = {
                    url : 'http://bgp.he.net/AS'+a[apn]+'#_prefixes',
                    headers:  {
                        'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
                    }
                };
                request(options, (function(apn) { return function(err, resp, body) {
                        var $ = cheerio.load(body);
                        var x = $('#table_prefixes4');
                        console.log("*"+x.text());
                            $('#table_prefixes4 tr').each(function(index, prefix) {
                            $(this).find('.nowrap').each(function(){
                            event = $(this).text().trim();
                            nextevent = $(this).next().text().trim();
                            console.log(apn+","+event+","+nextevent)
                            var intoquery = squel.insert().into("secondastable").set("asnumber",a[apn]).set("prefixes",event).set("description",nextevent);
                            console.log(intoquery.toString())
                            client.query(intoquery.toString());
                            });        
                            });
                }})(apn))
            }
    }        
});

0 个答案:

没有答案