【问题标题】:how to list column in mysql on nodejs with module mysql如何使用模块mysql在nodejs上列出mysql中的列
【发布时间】:2013-10-29 01:50:25
【问题描述】:

我想在 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,我会得到类似的结果。

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    您首先打印“字段”,这是SHOW COLUMNS 响应中字段的描述。响应行本身如下所示:

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

    所以第一个列名,例如是:

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

    【讨论】:

      猜你喜欢
      • 2014-08-04
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2017-01-21
      • 2014-07-29
      • 1970-01-01
      • 2017-12-19
      • 2016-06-25
      相关资源
      最近更新 更多