LoginSignup
10
9

More than 5 years have passed since last update.

node-sqlite3でプレースホルダ使用時に実際のSQLを確認する

Posted at

node-sqlite3でプレースホルダを使用している時に、実際に実行されているSQL文を確認したい。
そんな時は"trace"にコールバックを登録する。

trace.js
var sqlite = require("sqlite3").verbose();
var db = new sqlite.Database("test.db");

db.on("trace", function(sql) {
  console.log(sql);
});

obj = {
    title: "6月に自主制作を行いました!",
    type: "develop"
};

db.all(
    "SELECT * FROM entries where title=? and type=?",
    [obj.title, obj.type],
    function(error, rows) {
        rows.forEach(function(row){
            console.log("title :" + row.title + ", type:" + row.type);
        });
    }
);

実行すると

SELECT * FROM entries where title='6月に自主制作を行いました!' and type='develop'
title :6月に自主制作を行いました!, type:develop

って出力される。上の方がtraceで出してるSQL

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9