0

I am trying to use SQLITE3 to store data for my discord bot and here i need to save a number that goes up each time a command is used. Here is my code: (creating table and setting the value to 0.)

  db.run("CREATE TABLE gbanCases ( number INT )")
  db.run("INSERT INTO gbanCases (number) VALUES ($number)", {
    $number : 0
});

Trying to see what number returns:

        var caseNumber = db.run("SELECT number FROM gbanCases")
        console.log(caseNumber)

The thing is that caseNumber returns me "[object Object]" and I want it to return the 0 that was setted up earlier. Please help me if you can, i'm still very new to this.

CryZz
  • 5
  • 3
  • Replace your current log with `console.log({ caseNumber })` and see what you get. – code Aug 05 '22 at 17:46
  • Here is what i get: ```{caseNumber: Database} caseNumber: Database {filename: 'database.sqlite', mode: 65542} [[Prototype]]: Object``` – CryZz Aug 05 '22 at 17:51
  • Don't use `db.run()` for a `SELECT` query, use `db.get()` or `db.all()` depending on whether you want 1 row or all rows. They take a callback function that receives the results. – Barmar Aug 05 '22 at 17:54
  • how can i fix it? sorry if im asking to much, im new – CryZz Aug 05 '22 at 17:54

1 Answers1

1

Use db.get() to get the result of a query, not db.run(). The resulting row is passed as an argument to the callback function.

db.get("SELECT number FROM gbanCases", function(err, row) {
    if (err) {
        throw err;
    }
    console.log(row.number);
})
Barmar
  • 741,623
  • 53
  • 500
  • 612