SORU
9 ŞUBAT 2012, PERŞEMBE


Nasıl Node.js İsim üzerinden bağlantı yapmak için

Ben kendimi bulmak için çalışırken oluşturmak için bir PostgreSQL veritabanı, PostgreSQL kurdum ve başladı bir server initdb /usr/local/pgsql/data, o zaman başladı bu örneği ile postgres -D /usr/local/pgsql/data Şimdi nasıl yapabilirim etkileşim ile bu düğüm? Örneğin, connectionstring, ya da ne olduğunu bulmak mümkün. ne olurdu

CEVAP
9 ŞUBAT 2012, PERŞEMBE


İşte benim İsim için node.js veritabanına bağlanmak için kullandığım bir örnek.

Kullandığım node.js arabirim 5* *burada bulabilirsiniz

var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword@localhost:5432/YourDatabase";

var client = new pg.Client(conString);
client.connect();

//queries are queued and executed one after another once the connection becomes available
var x = 1000;

while(x>0)
{
client.query("INSERT INTO junk(name, a_number) values('Ted',12)");
client.query("INSERT INTO junk(name, a_number) values($1, $2)", ['John', x]);
x = x - 1;
}

var query = client.query("SELECT * FROM junk");
//fired after last row is emitted

query.on('row', function(row) {
  console.log(row);
});

query.on('end', function() { 
  client.end();
});



//queries can be executed either via text/parameter values passed as individual arguments
//or by passing an options object containing text, (optional) parameter values, and (optional) query name
client.query({
  name: 'insert beatle',
  text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
  values: ['George', 70, new Date(1946, 02, 14)]
});

//subsequent queries with the same name will be executed without re-parsing the query plan by postgres
client.query({
  name: 'insert beatle',
  values: ['Paul', 63, new Date(1945, 04, 03)]
});
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['john']);

//can stream row results back 1 at a time
query.on('row', function(row) {
  console.log(row);
  console.log("Beatle name: %s", row.name); //Beatle name: John
  console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
  console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height); //integers are returned as javascript ints
});

//fired after last row is emitted
query.on('end', function() { 
  client.end();
});

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ETrade Supply

    ETrade Suppl

    23 Temmuz 2011
  • Le Cargo !

    Le Cargo !

    24 HAZİRAN 2007
  • WoodysGamertag

    WoodysGamert

    17 Aralık 2009