-1

I have let usersin1 = sql.prepare("SELECT COUNT(*) FROM raid WHERE raid1 > 0");

using better-sqlite3 and what I'm getting from this is a [object Statement]. I don't understand why I'm getting it when I try to get the value of usersin1. All values are defined and I'm trying to get a number out of this.

Node.js

leetom
  • 723
  • 1
  • 6
  • 27
king
  • 127
  • 1
  • 7
  • A statement is just a representation of the actual database statement to be run. After that you need to run it - check here https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#class-statement ie., should be something like `sql.prepare("SELECT COUNT(*) FROM raid WHERE raid1 > 0").get()` – Afonso Tsukamoto Apr 17 '20 at 03:42

2 Answers2

2

sql.prepare() returns a Statement object. You need to execute the statement to get the results.

let stmt = sql.prepare("SELECT COUNT(*) count FROM raid WHERE raid1 > 0");
let row = stmt.get();
let usersin1 = row.count;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • If that's the case then why wouldn't something like `let usersin2 = sql.prepare("SELECT COUNT(*) FROM raid WHERE raid2 > 0").get().count;` do the same thing – king Apr 16 '20 at 23:42
  • 1
    Yes, you could do that. It's just not as clear for explaining all the steps. – Barmar Apr 16 '20 at 23:43
  • This may not work since there will be no `count` key on the `row` object. There will be `COUNT(*)` key though. In order to simplify it and make your code work, the query should state `SELECT COUNT(*) AS count FROM raid WHERE raid1 > 0` – Kyeno Mar 02 '23 at 03:19
  • @Kyeno I have the alias `count` in my SQL. The keyword `AS` is optional in SQL. – Barmar Mar 02 '23 at 16:40
-1

You can do : let usersin1 = sql.prepare("SELECT COUNT(*) FROM raid WHERE raid1 > 0").get().count;