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, 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 pieces of test data. The data inserted here is: [SQL] create table teacher (id varchar (10), name varchar (20), pwd varchar (10 )); [SQL] insert into teacher values ('1', 'aaa', '000000'); insert into teacher values ('2', 'bbb', '2016 '); insert into teacher values ('3', 'ccc ', '123'); insert into teacher values ('4', 'ddd', '123'); 1) client. js [cpp] 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 NAME = 'ipone' where ID = '4 '"; deleteSQLString = "delete from teacher where ID = '10'"; client. connect (function (error, results) {if (error) {console. log ('clientconnectionready Error: '+ error. message); client. end (); return;} console. log ('ing ing to S... '); console. log ('connectedto S automatically. '); console. log ('Connection success... \ n'); f. _ select (client, selectSQLString); f. _ insert (client, insertSQLString, value); f. _ select (client, selectSQLString); f. _ delete (client, deleteSQLString) ;}); 2) function. js [cpp] <span style = "font-size: 14px;"> <span style = "font-size: 12px;"> function _ insert (client, insertSQLString, value) {console. log ("insert beginning"); client. query (insertSQLString, value, function (error, results) {if (error) {console. log ("ClientReady Error:" + error. message), client. end (); return;} console. log ('inserted: '+ results. affectedRows + 'row. '), console. log ('insert success... \ n') ;}); console. log ("insert end \ n");} function _ select (client, selectSQLString) {console. log ("select beginning"); client. query (selectSQLString, function selectCb (error, results, fields) {console. log ("in select callback function"); if (error) {console. log ('getdata Error: '+ error. message), client. end (); return;} if (results. rowCount> 0) {var firstResult, resultSet = ''; for (var I = 0, len = results. rowCount; I <len; I ++) {firstResult = results. rows [I]; resultSet + = 'id: '+ firstResult ['id'] + ''+ 'name: '+ firstResult ['name'] + ''+ 'pwd:' + firstResult ['pwd'] + '\ n';} console. log (resultSet);/* Add function: Make the query result set return to the client and ensure the universality of this function. */}); console. log ("select end \ n");} function _ update (client, updateSQLString) {console. log ("update beginning"); client. query (updateSQLString, function (error, results) {if (error) {console. log ("ClientReady Error:" + error. message), client. end (); return;} console. log ('Update success... \ n') ;}); console. log ("update end \ n");} function _ delete (client, deleteSQLString) {console. log ("delete beginning"); client. query (deleteSQLString, function (error, results) {if (error) {console. log ("ClientReady Error:" + error. message), client. end (); return;} console. log ('delete success... \ n') ;}); console. log ("delete end \ n");} exports. _ insert = _ insert; exports. _ select = _ select; exports. _ update = _ update; exports. _ delete = _ delete; </span> 3) Run node client. js, the result is as follows: Connecting to S... connected to S automatically. connection success... select beginningselect end insert beginninginsert end select beginningselect end delete beginningdelete end in select callback functionid: 1 name: aaa pwd: 111id: 2 name: bbb pwd: 222id: 3 name: ccc pwd: 333id: 4 name: ddd pwd: 444 Inserted: undefined row. insert success... in select callback functionid: 1 name: aaa pwd: 111id: 2 name: bbb pwd: 222id: 3 name: ccc pwd: 333id: 4 name: ddd pwd: 444id: 10 name: fillp pwd: abc delete success... in the function implementation, we adopt the callback function form for implementation. According to the output information, we can clearly see the node. main features of js: non-blocking

Related Article

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.