TryGhost / node-sqlite3

SQLite3 bindings for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

JiaJiaJiang opened this issue · comments

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:

if (stmt->status != SQLITE_DONE || baton->parameters.size()) {

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