0

I have below code to insert the values into database

db.one('INSERT INTO transactions (transaction_id) VALUES (a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11)')
    .then(user => {
        console.log(user.name); // print user name;
    })
    .catch(error => {
        console.log(error, 'jjjjjjjjjjjj'); // print the error;
    });

I want to insert three fields

1) transaction_id 
2) amount 
3) currencies_currency_id

But getting error

error: syntax error at or near "c0b"

Please help what I am doing wrong here

4
  • If you want to insert3 fields then you should specify those 3 columns and strings should be single-quoted. INSERT INTO transactions (transaction_id,amount, currencies_currency_id) VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', ?,?) Commented Jan 8, 2019 at 6:39
  • Thank you but Giving me error error: syntax error at or near "amount" Commented Jan 8, 2019 at 6:54
  • what's db.one ? it's not part of PostgreSQL. which language is that ? tag that as well. My query should work in PostgreSQL when you substitute other values (?,?) correctly. I don't know how it works in your programming language. Commented Jan 8, 2019 at 6:55
  • @KaushikNayak It is nodejs Commented Jan 8, 2019 at 7:10

1 Answer 1

3

I think SQL value needs single quotes

try

db.one('INSERT INTO transactions (transaction_id, amount, currencies_currency_id) VALUES (\'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11\', \'enter amount\', \'enter currencies_currency_id\')')
.then(user => {
    console.log(user.name); // print user name;
})
.catch(error => {
    console.log(error, 'jjjjjjjjjjjj'); // print the error;
});
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you how can I insert second field?
Hi I need to call one for query to get details for particular user_id "SELECT * from wallet WHERE users_details_iwant_user_id = 1" But this doesn't work
When building sql statements in nodejs, I think you'll find it easier to use ES6 template literals. Also try typecasting your values to the types of the columns they are intended for.
@FirozShams ES6 template literals. What are that?
Using template literals your query can be rewritten as INSERT INTO transactions (transaction_id, amount, currencies_currency_id) VALUES ('${"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"}', ${29},'${"some currency id"}')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.