javascript 如何使用模块mysql在nodejs上列出mysql中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19648258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:20:12  来源:igfitidea点击:

how to list column in mysql on nodejs with module mysql

javascriptmysqlnode.js

提问by goten

I want to list columns in a table using module mysql on nodejs

我想在 nodejs 上使用模块 mysql 列出表中的列

When I run the query :

当我运行查询时:

SHOW COLUMNS FROM tableName WHERE FIELD = columnName

It's work fine, I can know if the column exist or not.

它工作正常,我可以知道该列是否存在。

But I want to list the columns and I get a list of object and I don't what to do with that and if I get the good result.

但我想列出列,我得到一个对象列表,我不知道该怎么做,如果我得到了好的结果。

I tried :

我试过 :

SHOW COLUMNS FROM tableName
DESCRIBE tableName

with both queries I get a list of object

通过这两个查询,我得到了一个对象列表


{ 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 }

The function I use is the following :

我使用的功能如下:

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);
});

If someone have a solution for me I tried also to look in information_schema and get the same result. Thanks you in advance.

如果有人对我有解决方案,我也尝试查看 information_schema 并获得相同的结果。提前谢谢你。

At the same time if someone can tell how to use show table I get a similar result.

同时,如果有人能告诉我如何使用 show table,我会得到类似的结果。

回答by Andrey Sidorov

You are printing 'fields' first, which is description of fields in the SHOW COLUMNSresponse. Response rows itself looks like this:

您首先打印“字段”,这是SHOW COLUMNS响应中字段的描述。响应行本身看起来像这样:

 [
  {
    Field: 'id',
    Type: 'int(11) unsigned',
    Null: 'NO',
    Key: 'PRI',
    Default: null,
    Extra: 'auto_increment'
  }
  /* , ... */
 ]

So first column name, for example is:

例如,第一个列名是:

connection.query('SHOW COLUMNS FROM test', function(err, rows, fields){ 
    console.log(rows[0].Field);
});