TryGhost / node-sqlite3

SQLite3 bindings for Node.js
BSD 3-Clause "New" or "Revised" License
6.23k stars 817 forks source link

Incorrect/broken example in documentation regarding parameter binding #1711

Open mvduin opened 1 year ago

mvduin commented 1 year ago

Issue Summary

The documentation for Database#run states:

      db.run("UPDATE tbl SET name = ?5 WHERE id = ?", {
          1: 2,
          5: "bar"
      });

This binds the first placeholder ($id) to 2 and the placeholder with index 5 to "bar".

This is incorrect, to quote the sqlite documentation:

A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned.

Therefore the ? will be parameter 6, not parameter 1.

Steps to Reproduce

Demonstration:

'use strict';

const { promisify } = require('util');
const events = require('events');
const sqlite3 = require('sqlite3');

(async () => {
    let db = new sqlite3.Database(':memory:');
    await events.once(db, 'open');

    db.get_p = promisify(db.get);

    console.log( await db.get_p(`SELECT ?5, ?`, { 1: 2, 5: "bar" }) );
    console.log( await db.get_p(`SELECT ?5, ?`, { 6: 2, 5: "bar" }) );
    console.log( await db.get_p(`SELECT ?5, ?`, 1, 2, 3, 4, 5, 6) );
})();

output:

{ '?5': 'bar', '?': null }
{ '?5': 'bar', '?': 2 }
{ '?5': 5, '?': 6 }

Version

n/a

Node.js Version

n/a

How did you install the library?

n/a