42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
|
|
const mysql = require('mysql2');
|
||
|
|
|
||
|
|
async function checkTables() {
|
||
|
|
const connection = mysql.createConnection({
|
||
|
|
host: process.env.DB_HOST || 'nj-cdb-3pwh2kz1.sql.tencentcdb.com',
|
||
|
|
port: process.env.DB_PORT || 20784,
|
||
|
|
user: process.env.DB_USER || 'jiebanke',
|
||
|
|
password: process.env.DB_PASSWORD || 'aiot741$12346',
|
||
|
|
database: process.env.DB_NAME || 'niumall'
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
console.log('连接到远程数据库...');
|
||
|
|
|
||
|
|
// 获取所有表名
|
||
|
|
const [tables] = await connection.promise().query('SHOW TABLES');
|
||
|
|
console.log('数据库中的表:');
|
||
|
|
tables.forEach(table => {
|
||
|
|
const tableName = table[`Tables_in_${process.env.DB_NAME || 'niumall'}`];
|
||
|
|
console.log(`- ${tableName}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 获取每个表的详细信息
|
||
|
|
console.log('\n表结构详情:');
|
||
|
|
for (const table of tables) {
|
||
|
|
const tableName = table[`Tables_in_${process.env.DB_NAME || 'niumall'}`];
|
||
|
|
console.log(`\n表 ${tableName}:`);
|
||
|
|
|
||
|
|
const [columns] = await connection.promise().query(`DESCRIBE ${tableName}`);
|
||
|
|
columns.forEach(column => {
|
||
|
|
console.log(` ${column.Field} (${column.Type}) ${column.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${column.Key ? `KEY: ${column.Key}` : ''} ${column.Default ? `DEFAULT: ${column.Default}` : ''}`);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('数据库连接错误:', error.message);
|
||
|
|
} finally {
|
||
|
|
await connection.end();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
checkTables();
|