You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.2 KiB
67 lines
2.2 KiB
|
3 months ago
|
require('dotenv').config();
|
||
|
|
const mysql = require('mysql2/promise');
|
||
|
|
|
||
|
|
// 查询用户信息
|
||
|
|
async function listUsers() {
|
||
|
|
let connection = null;
|
||
|
|
try {
|
||
|
|
console.log('连接数据库...');
|
||
|
|
connection = await mysql.createConnection({
|
||
|
|
host: process.env.DB_HOST || 'localhost',
|
||
|
|
user: process.env.DB_USER || 'root',
|
||
|
|
password: process.env.DB_PASSWORD === undefined ? null : process.env.DB_PASSWORD,
|
||
|
|
database: process.env.DB_DATABASE || 'wechat_app',
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
||
|
|
});
|
||
|
|
console.log('数据库连接成功\n');
|
||
|
|
|
||
|
|
// 查询users表结构
|
||
|
|
console.log('=== users表结构 ===');
|
||
|
|
const [columns] = await connection.execute(
|
||
|
|
'SHOW COLUMNS FROM users'
|
||
|
|
);
|
||
|
|
console.log('字段列表:', columns.map(col => col.Field).join(', '));
|
||
|
|
console.log();
|
||
|
|
|
||
|
|
// 查询前5个用户数据
|
||
|
|
console.log('=== 用户数据 (前5个) ===');
|
||
|
|
const [users] = await connection.execute(
|
||
|
|
'SELECT * FROM users LIMIT 5'
|
||
|
|
);
|
||
|
|
console.log(`数据库中有 ${users.length} 个用户记录`);
|
||
|
|
|
||
|
|
if (users.length > 0) {
|
||
|
|
users.forEach((user, index) => {
|
||
|
|
console.log(`${index + 1}. 用户数据:`, user);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 查找可能的openid字段
|
||
|
|
const firstUser = users[0];
|
||
|
|
const possibleOpenIdFields = Object.keys(firstUser).filter(key =>
|
||
|
|
key.toLowerCase().includes('openid') || key.toLowerCase().includes('open_id')
|
||
|
|
);
|
||
|
|
|
||
|
|
if (possibleOpenIdFields.length > 0) {
|
||
|
|
console.log('\n=== 可能的openid字段 ===');
|
||
|
|
console.log('找到以下可能的openid字段:', possibleOpenIdFields.join(', '));
|
||
|
|
console.log('请使用其中一个有效的字段更新测试脚本');
|
||
|
|
} else {
|
||
|
|
console.log('\n未找到明显的openid字段,请检查users表结构后手动更新测试脚本');
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log('\n数据库中没有用户记录');
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('查询过程中发生错误:', error);
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
await connection.end();
|
||
|
|
console.log('\n数据库连接已关闭');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行查询
|
||
|
|
listUsers();
|