如何使用模块mysql在nodejs上的mysql中列出列 [英] how to list column in mysql on nodejs with module mysql

查看:193
本文介绍了如何使用模块mysql在nodejs上的mysql中列出列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我运行查询时:

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.

我尝试过:

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 }

我使用的功能如下:

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并获得相同的结果.预先谢谢你.

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

同时,如果有人可以告诉我如何使用show table,我也会得到类似的结果.

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

推荐答案

您首先要打印字段",这是对SHOW COLUMNS响应中字段的描述.响应行本身看起来像这样:

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

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

这篇关于如何使用模块mysql在nodejs上的mysql中列出列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆