在db / index.js中:
const { Pool } = require('pg'); //node-postgres
const pool = new Pool;
module.exports = {
query: (text, params, callback) => {
return pool.query(text,params,callback);
}
};
在我的main.js中:
const db = require('./db/index');
db.query('SELECT count(*) from pg_class where relname ="session" and relkind="r"', (err, res) => {
if (err) {
//TODO: What to do if there is an error?
}
console.log(res);
});
输出结果为:
未定义
如何查看名称为" session"的表格?存在与否?我如何知道我的查询是否有效?
编辑:这不是上述问题的重复,因为我的问题与Node.js的javascript代码有关。不是SQL!我只是想知道在res对象中要查找什么,因为它似乎是未定义的......
答案 0 :(得分:3)
您可以查看以下代码;
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'schema_name'
AND table_name = 'table_name'
);
或
SELECT EXISTS (
SELECT 1
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'schema_name'
AND c.relname = 'table_name'
);
或
SELECT 'schema_name.table_name'::regclass
或
SELECT to_regclass('schema_name.table_name');
如果不存在,则此查询返回null