Node. js operations on PostgreSQL databases

Source: Internet
Author: User
This node. js operation on the postgresql database is implemented using two files. One file is equivalent to a client that connects to the database and calls corresponding functions.

This node. js operation on the postgresql database is implemented using two files. One file is equivalent to a client that connects to the database and calls corresponding functions.

In the previous article, node. js accessed the postgresql database and implemented database operations in the same file. See

This node. js operation on the postgresql database is implemented using two files. One file is equivalent to a client, which connects to the database and calls the corresponding function. The other file is used to implement the function.

Create a table named teacher in the database based on the node. js, postgresql database (not necessarily local), and corresponding modules installed. Insert several test data records. The test data inserted here is:

Create table teacher (id varchar (10), name varchar (20), pwd varchar (10 ));

Insert into teacher values ('1', 'aaa', '20140901 ');
Insert into teacher values ('2', 'bbb ', '123 ');
Insert into teacher values ('3', 'ccc ', '123 ');
Insert into teacher values ('4', 'ddd ', '123 ');

1) client. js

Var f = require ('./function ');
Var pg = require ('pg ');

Var conString = "tcp: // s: postgres @ localhost/my ";

Var client = new pg. Client (conString );

Var value = ['10', 'fillp ', 'abc'];
InsertSQLString = 'insert into teacher values ($1, $2, $3 )';
SelectSQLString = 'select * from teacher ';
UpdateSQLString = "update teacher set where ";
DeleteSQLString = "delete from teacher where ";

Client. connect (function (error, results ){
If (error ){
Console. log ('clientconnectionready Error: '+ error. message );
Client. end ();
Return;
}
Console. log ('ing ing to S ...');
Console. log ('ted CTED to S automatically .');
Console. log ('Connection success... \ n ');

F. _ select (client, selectSQLString );
F. _ insert (client, insertSQLString, value );
F. _ select (client, selectSQLString );
F. _ delete (client, deleteSQLString );
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.