mysql_import_data
Import data from CSV or JSON files into MySQL tables. Use the tool to specify table name, file path, format, and optional truncation to streamline data loading processes.
Instructions
从文件导入数据到表
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | 导入文件路径 | |
| format | Yes | 文件格式 | |
| tableName | Yes | 表名称 | |
| truncateFirst | No | 是否先清空表 |
Implementation Reference
- src/server.ts:490-531 (handler)The main handler function `handleImportData` that executes the mysql_import_data tool. It optionally truncates the table, imports data from the file using the `importData` helper, generates INSERT statements, and executes them via the database manager.private async handleImportData(args: { tableName: string; filePath: string; format: 'csv' | 'json'; truncateFirst?: boolean }): Promise<any> { if (args.truncateFirst) { await this.dbManager.query(`TRUNCATE TABLE \`${args.tableName}\``); } const data = await importData({ format: args.format, filePath: args.filePath, tableName: args.tableName, }); if (data.length === 0) { return { content: [ { type: 'text', text: '导入文件为空,没有数据需要导入', }, ], }; } const columns = Object.keys(data[0]); const placeholders = columns.map(() => '?').join(', '); let importedRows = 0; for (const row of data) { const values = columns.map(col => row[col]); const sql = `INSERT INTO \`${args.tableName}\` (\`${columns.join('`, `')}\`) VALUES (${placeholders})`; const result = await this.dbManager.query(sql, values); importedRows += result.affectedRows || 0; } return { content: [ { type: 'text', text: `成功导入 ${importedRows} 行数据到表 ${args.tableName}`, }, ], }; }
- src/server.ts:210-223 (schema)Tool schema definition for `mysql_import_data` including input parameters and validation rules.{ name: 'mysql_import_data', description: '从文件导入数据到表', inputSchema: { type: 'object', properties: { tableName: { type: 'string', description: '表名称' }, filePath: { type: 'string', description: '导入文件路径' }, format: { type: 'string', enum: ['csv', 'json'], description: '文件格式' }, truncateFirst: { type: 'boolean', description: '是否先清空表', default: false }, }, required: ['tableName', 'filePath', 'format'], }, },
- src/server.ts:261-262 (registration)Registration of the `mysql_import_data` tool handler in the switch statement for tool calls.case 'mysql_import_data': return await this.handleImportData(args as any);
- src/utils.ts:40-59 (helper)Helper function `importData` that parses and returns data from JSON or CSV files, used by the mysql_import_data handler.export async function importData(options: ImportOptions): Promise<any[]> { const { format, filePath } = options; if (format === 'json') { const content = await fs.readFile(filePath, 'utf-8'); return JSON.parse(content); } else if (format === 'csv') { return new Promise((resolve, reject) => { const results: any[] = []; createReadStream(filePath) .pipe(csvParser()) .on('data', (data: any) => results.push(data)) .on('end', () => resolve(results)) .on('error', reject); }); } throw new Error(`不支持的格式: ${format}`); }