-1

Is there any way to make this piece of SQLite3 code shorter & more efficient? I'd like to add integers to an existing integer in a column.

I've tried the code below.

(db is defined as my SQLite3 db connection)

db.all(`SELECT * FROM coins WHERE userId = ?`, '28978936', async (err, resp) => {

if (resp.length === 0) resp[0].balance = 0;

var earnings = resp[0].balance + 100;

  db.run(`INSERT INTO coins (userId, balance) VALUES (?, ?)`, '28978936', earnings, (err) => {

    console.log('100 coins have been added to user "28978936"')
  })
})

The code worked for me, however is there a better way to do it?

haste
  • 99
  • 5

1 Answers1

0

Please execute the below query

UPDATE coins SET balance = balance + 100 WHERE userId = ?
GRS
  • 1,829
  • 1
  • 9
  • 23