TryGhost / node-sqlite3

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

A used stmt can not get result by get() when there is no parameter #1743

Open JiaJiaJiang opened 10 months ago

JiaJiaJiang commented 10 months ago

Issue Summary

I created a stmt instance by db.prepare, then I found the 'get' method of stmt cannot return correct result but undefined after the first time it was called when there is no parameter for this method.

I think this bug is caused by the if condition here:https://github.com/TryGhost/node-sqlite3/blob/03d6e75bc6afcac66cc09e1e6ffadf59874195fb/src/statement.cc#L406

Steps to Reproduce

const sqlite3 = require('sqlite3');
function promiseify(fn) {
    return function (...args) {
        return new Promise((resolve, reject) => {
            fn(...args, (err, res) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(res);
                }
            });
        });
    };
}
const db = new sqlite3.Database(':memory:');
(async () => {
    //no parameter test
    const stmt = await db.prepare('select 1'); //simply select 1 here
    const get = promiseify((cb) => {
        return stmt.get(cb);
    });
    console.log(await get()); //print { '1': 1 }
    console.log(await get()); //print undefined
    console.log(await get()); //print undefined
    //You can see the `get` method won't return a result from the second call.

    //1 parameter test
    const stmt2 = await db.prepare('select 1+?'); 
    const get2 = promiseify((arg,cb) => {
        return stmt2.get(arg,cb);
    });
    console.log(await get2(1)); //print { '1+?': 2 }
    console.log(await get2(2)); //print { '1+?': 3 }
    console.log(await get2(3)); //print { '1+?': 4 }
    //it returns correctly when there have parameter
})();

Version

5.1.6

Node.js Version

v18.17.0

How did you install the library?

npm i, on windows