mysql_insert
Add data to MySQL database tables by specifying the table name and data array. This tool enables inserting records into MySQL databases through structured data input.
Instructions
插入数据到表
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tableName | Yes | 表名称 | |
| data | Yes | 要插入的数据数组 |
Implementation Reference
- src/server.ts:409-433 (handler)The handler function that implements the 'mysql_insert' tool logic. It dynamically generates INSERT SQL from the table name and data array, executes batched inserts using parameterized queries via DatabaseManager, and returns the number of affected rows.private async handleInsert(args: { tableName: string; data: any[] }): Promise<any> { if (!Array.isArray(args.data) || args.data.length === 0) { throw new Error('数据必须是非空数组'); } const columns = Object.keys(args.data[0]); const placeholders = columns.map(() => '?').join(', '); const values = args.data.map(row => columns.map(col => row[col])); let affectedRows = 0; for (const rowValues of values) { const sql = `INSERT INTO \`${args.tableName}\` (\`${columns.join('`, `')}\`) VALUES (${placeholders})`; const result = await this.dbManager.query(sql, rowValues); affectedRows += result.affectedRows || 0; } return { content: [ { type: 'text', text: `成功插入 ${affectedRows} 行数据到表 ${args.tableName}`, }, ], }; }
- src/server.ts:155-170 (schema)The schema definition for the 'mysql_insert' tool, including input schema specifying tableName (string) and data (array of objects). This is part of the tools list returned by ListToolsRequest.{ name: 'mysql_insert', description: '插入数据到表', inputSchema: { type: 'object', properties: { tableName: { type: 'string', description: '表名称' }, data: { type: 'array', description: '要插入的数据数组', items: { type: 'object' } }, }, required: ['tableName', 'data'], }, },
- src/server.ts:253-254 (registration)Registration of the 'mysql_insert' tool handler in the switch statement within the CallToolRequestSchema handler, dispatching to the handleInsert method.case 'mysql_insert': return await this.handleInsert(args as any);