2013-10-29 13 views
0

我想使用上的NodeJS如何在mysql中列出列與模塊的MySQL

模塊的mysql列出表中的列的NodeJS當我運行查詢:

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我會得到類似的結果。

回答

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