1

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.

4
  • Replace your current log with console.log({ caseNumber }) and see what you get. Commented Aug 5, 2022 at 17:46
  • Here is what i get: {caseNumber: Database} caseNumber: Database {filename: 'database.sqlite', mode: 65542} [[Prototype]]: Object Commented Aug 5, 2022 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. Commented Aug 5, 2022 at 17:54
  • how can i fix it? sorry if im asking to much, im new Commented Aug 5, 2022 at 17:54

1 Answer 1

2

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);
})
Sign up to request clarification or add additional context in comments.

15 Comments

console.log(row.number); returns : "{}1"
I suspect {} is coming from something else, not this. row.number shouldn't contain that.
How can I fix it? I'm supposed to get 0.
I can't think of any reason why it doesn't work. The {} is not coming from my code, it must be coming from something else you're doing.
Maybe i didn't do this correctly : ` db.run("CREATE TABLE gbanCases ( number INT )") db.run("INSERT INTO gbanCases (number) VALUES ($number)", { $number : 0 });`
|

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.