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 );
});