Pte*_*tyl 4 postgresql node.js node-postgres
在 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);
}
};
Run Code Online (Sandbox Code Playgroud)
在我的 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);
});
Run Code Online (Sandbox Code Playgroud)
输出是:
不明确的
如何检查名称为“session”的表是否存在?我怎么知道我的查询是否有效?
编辑:这不是上述问题的重复,因为我的问题与 Node.js javascript 代码有关。不是 SQL!我只想知道要在 res 对象中查找什么,因为它似乎未定义...
您可以查看以下代码;
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'schema_name'
AND table_name = 'table_name'
);
Run Code Online (Sandbox Code Playgroud)
或者
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'
);
Run Code Online (Sandbox Code Playgroud)
或者
SELECT 'schema_name.table_name'::regclass
Run Code Online (Sandbox Code Playgroud)
或者
SELECT to_regclass('schema_name.table_name');
Run Code Online (Sandbox Code Playgroud)
如果不存在此查询返回 null
| 归档时间: |
|
| 查看次数: |
8773 次 |
| 最近记录: |