我想在 nodejs 上使用模块 mysql 列出表中的列
当我运行查询时:
SHOW COLUMNS FROM tableName WHERE FIELD = columnName
它工作正常,我可以知道该列是否存在。
但我想列出这些列,我得到一个对象列表,我不知道该怎么做,如果我得到了好的结果。
我试过了 :
SHOW COLUMNS FROM tableName
DESCRIBE tableName
通过这两个查询,我得到了一个对象列表
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Field',
orgName: 'COLUMN_NAME',
filler1: ,
charsetNr: 33,
length: 192,
type: 253,
flags: 1,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Type',
orgName: 'COLUMN_TYPE',
filler1: ,
charsetNr: 33,
length: 589815,
type: 252,
flags: 17,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Null',
orgName: 'IS_NULLABLE',
filler1: ,
charsetNr: 33,
length: 9,
type: 253,
flags: 1,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Key',
orgName: 'COLUMN_KEY',
filler1: ,
charsetNr: 33,
length: 9,
type: 253,
flags: 1,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Default',
orgName: 'COLUMN_DEFAULT',
filler1: ,
charsetNr: 33,
length: 589815,
type: 252,
flags: 16,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
{ catalog: 'def',
db: 'information_schema',
table: 'COLUMNS',
orgTable: 'COLUMNS',
name: 'Extra',
orgName: 'EXTRA',
filler1: ,
charsetNr: 33,
length: 90,
type: 253,
flags: 1,
decimals: 0,
filler2: ,
default: undefined,
zeroFill: false,
protocol41: true }
我使用的功能如下:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : "host",
user : "user",
password : "pass",
database : "db"
});
connection.query(myQuery, function(err, rows, fields){
if(err) console.log(err);
if(fields) console.log(fields);
if(rows) console.log(rows);
});
如果有人对我有解决方案,我也尝试查看 information_schema 并获得相同的结果。提前谢谢你。
同时,如果有人能告诉我如何使用 show table,我会得到类似的结果。