how to insert and update data into postgresql from node js using if condition

katy

i have to "UPDATE" data in postgresql if data is already present in database and "INSERT" i.e create a new user_Id and insert data using IF condition. i have tried with this but i am not getting output. please help me if you know .

if(data.Details){
    db.query('UPDATE Details SET fullName = $2,address= $3,phone = $4 WHERE user_id = $1 RETURNING *', [query, data.Details.fullName, data.Details.address, data.Details.phone],function(err,details) {
                if (err) return callback(new Error('error'));
             })
}else{
db.query('INSERT INTO Details(user_id,fullName,address,phone) VALUES($1,$2,$3,$4) RETURNING *', [query, data.Details.fullName, data.Details.address, data.Details.phone],function(err,details) {
                                if (err) return callback(new Error('error'));

                            })
}
Horia Coman

If you want to get fancy, you can also use UPSERT/ON CONFLICT in Postgres 9.5 and later.

It is designed for this exact use case, and executes as a single "instruction", rather than having to do a query check whether something exists, and another one to update or insert, both in a transaction.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Update Temp table to Insert node in XML data using SQL

分類Dev

How to insert and retrieve image from PostgreSql using C#

分類Dev

How to implement callbacks on AWS RDS PostgreSQL using node.js

分類Dev

JS: How to make logic (using loops) to insert data into this table?

分類Dev

Is there a way to directly insert data from a parquet file into PostgreSQL database?

分類Dev

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

分類Dev

How to inform and update a client when a new record inserted in mysql database using node js and socket.io

分類Dev

Insert or update data using cte_results in SQL Server

分類Dev

How to make two query to redis from node.js using promise from when.js

分類Dev

How to insert data into Microsoft Sql using powershell?

分類Dev

How to fetch data from array and insert to database

分類Dev

how to insert data from scrapy to mysql

分類Dev

how to insert data from 3 different tables into one table using the select command

分類Dev

How to insert data from excel file into microsoft localdb database using C#

分類Dev

Data deletion from database dynamically using delete request in node.js

分類Dev

Update Collection by using Data from that Collection in mongodb

分類Dev

In Core Data, how to do `if exists update else insert` in swift 4?

分類Dev

How to use like in where condition in sequelize, node js

分類Dev

Chart.js - How to update data

分類Dev

How to update row if exist and meet more than one condition and insert it otherwise in SQL Server

分類Dev

unable to update the array using pull in node.js

分類Dev

Node.js: How to send headers with form data using request module?

分類Dev

How to import collection and nested collection data in cloud firestore using node js?

分類Dev

how to send data using ajax post method?(node.js mogodb)

分類Dev

How to pass JSON data from Node js route to script inside an html file?

分類Dev

How should I return this remote JSON data from my Node.js module?

分類Dev

How to update data in entity framework using automapper?

分類Dev

How to Update Data using PHP MYSQL in Android

分類Dev

How to update data in Meteor using Reactivevar

Related 関連記事

  1. 1

    Update Temp table to Insert node in XML data using SQL

  2. 2

    How to insert and retrieve image from PostgreSql using C#

  3. 3

    How to implement callbacks on AWS RDS PostgreSQL using node.js

  4. 4

    JS: How to make logic (using loops) to insert data into this table?

  5. 5

    Is there a way to directly insert data from a parquet file into PostgreSQL database?

  6. 6

    SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

  7. 7

    How to inform and update a client when a new record inserted in mysql database using node js and socket.io

  8. 8

    Insert or update data using cte_results in SQL Server

  9. 9

    How to make two query to redis from node.js using promise from when.js

  10. 10

    How to insert data into Microsoft Sql using powershell?

  11. 11

    How to fetch data from array and insert to database

  12. 12

    how to insert data from scrapy to mysql

  13. 13

    how to insert data from 3 different tables into one table using the select command

  14. 14

    How to insert data from excel file into microsoft localdb database using C#

  15. 15

    Data deletion from database dynamically using delete request in node.js

  16. 16

    Update Collection by using Data from that Collection in mongodb

  17. 17

    In Core Data, how to do `if exists update else insert` in swift 4?

  18. 18

    How to use like in where condition in sequelize, node js

  19. 19

    Chart.js - How to update data

  20. 20

    How to update row if exist and meet more than one condition and insert it otherwise in SQL Server

  21. 21

    unable to update the array using pull in node.js

  22. 22

    Node.js: How to send headers with form data using request module?

  23. 23

    How to import collection and nested collection data in cloud firestore using node js?

  24. 24

    how to send data using ajax post method?(node.js mogodb)

  25. 25

    How to pass JSON data from Node js route to script inside an html file?

  26. 26

    How should I return this remote JSON data from my Node.js module?

  27. 27

    How to update data in entity framework using automapper?

  28. 28

    How to Update Data using PHP MYSQL in Android

  29. 29

    How to update data in Meteor using Reactivevar

ホットタグ

アーカイブ